Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4080552
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T17:56:41+00:00 2026-05-20T17:56:41+00:00

Quick preview of the problem: using .closest() , I’m able to stick a class

  • 0

Quick preview of the problem: using .closest(), I’m able to stick a class attribute on all instances of the target element, but it won’t insert the class value on any that have an extra <div> in the ancestor DOM.

First, here’s the jQuery:

// Throw class="errorWrap" on any <li> element that contains a field that doesn't validate
highlight: function(element) {
    $(element).closest("li").addClass("errorWrap");
}

(…The ‘highlight:’ is part of the Validate plug-in…)

And here’s a snippet of my HTML:

<li>
    <div>
        <span class="reqField">*</span> Have you been employed with us before?<br />
        <label class="radioInput"><input type="radio" name="app_prior_employ" value="Yes" /> Yes</label>
        <label class="radioInput"><input type="radio" name="app_prior_employ" value="No" /> No</label>
    </div>

    <div>
        <label for="app_prior_employ_date">If <em>yes</em>, when were you employed?</label><br />
        <input type="text" name="app_prior_employ_date" value="" size="40" id="app_prior_employ_date" />
    </div>
</li>

…now, with the above, the class attribute will be added to the opening <li> tag when it’s supposed to, but it’s value will be empty rather than errorWrap

As I mentioned at the top of this question, this issue occurs on only instances that have the <div> elements in the mix (as they do immediately above). This issue does not occur on other instances where the <div> is not in place, such as:

<li>
    <span class="reqField">*</span> Can you travel if a job requires it?<br />
    <label class="radioInput"><input type="radio" name="app_travel_for_job" value="Yes" /> Yes</label>
    <label class="radioInput"><input type="radio" name="app_travel_for_job" value="No" /> No</label>
</li>

I can’t figure out why jQuery would succeed in adding the class attribute everywhere I want it to, but fail to insert the value I’ve defined only in those that have an extra element in the DOM. Am I using .closest() improperly?

Added moments later: Here’s a marked-up screen shot of the issue: https://i.stack.imgur.com/aiiaX.png

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T17:56:42+00:00Added an answer on May 20, 2026 at 5:56 pm

    You’re using closest() properly, but maybe the context in which the highlight() function is wrong in that instance. In the scripts inspector, put a breakpoint inside the highlight function, and see if element is actually the input you think it is.

    Alternatively, you say the li has a class, but no value when the div is in the mix. Maybe the class is getting set then immediately unset. Using the script inspector and setting breakpoints will help you figure that out.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Quick question: what is the compiler flag to allow g++ to spawn multiple instances
Quick one, but thought I'd ask. Is there a better way of getting the
I'm running ASP.NET MVC 2 Preview 2 (With VS 2010 Beta 2) using Entity
I'd like to throw together a quick HTML preview window that takes the contents
Quick question, hopefully I am just missing something simple. Ok I have one class
I am having a problem trying to load an image after an element has
Quick background, I am using Netbeans to develop this (I don't have much experience
quick question really: I have this code: using System; using System.Collections.Generic; using System.Linq; using
Quick and should be simple question, but i cant find the answer!!! So im
quick question I know in wordpress you can get post_content,etc but let say I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.