4

In jquery can I combine "closest" or Parents() with "hasClass", so it will give me a closest element with given class, if it exists on a page?

var matchingDiv = $(this).closest('div').hasClass('SomeClass')

if ( matchingDiv != null )
{
   //we found matching element now manipulate it

}

I will always have either one matching div element, or none. Thanks.

7 Answers 7

5
var matchingDiv = $(this).closest('div.SomeClass')

if ( matchingDiv.length )
{
   // use length to find if there was a match.
   // closest() will always return an object.
}

If this doesn't work, perhaps post some html. Maybe there's a problem there?

Sign up to request clarification or add additional context in comments.

4 Comments

It does work, thanks a lot. Documentation states that it returns null, if no matching elements found
@Patrick, you could also shorten to just if (matchingDiv.length) since 0 will evaluate to false
@Doug, Very true. More concise and concise is good. Will correct.
@VictorS - Actually the documentation for closest() states ' The returned jQuery object contains zero or one element '. (Had to check on that one.) No matter what, you're getting an Object.
1

try this, closest takes a selector:

var matchingDiv = $(this).closest('div.SomeClass')

if ( matchingDiv != null )
{
   //we found matching element now manipulate it

}

5 Comments

I've tried it before it doesn't work for the reasons unknown to me, it should imo
strange, how about this: $(this).parents('div').find('.SomeClass:first')
This does not work. You test for null will always be true, even if there was no match.
That seems to be the case, how should I change test then to make it valid?
@VictorS - I've added an answer with the proper test.
1

Closest takes a selector so you can do this:

 var matchingDiv = $(this).closest('div.SomeClass')

Your next line is a problem, you should be checking a jQuery result like so:

if ( matchingDiv.length )
{
    //we found matching element now manipulate it
}

Comments

1

If you need to check whether the current element itself might be the ancestor you're looking for.. You can use a combination of is() and closest() as follows :

var matchingDiv = $(this).closest('.SomeClass');

matchingDiv = matchingDiv.length == 0 ? (matchingDiv.is('.SomeClass') ? matchingDiv : null) : matchingDiv

if ( matchingDiv != null )
{
   //found matching element 

}

Comments

0

Are you asking if this will work or not?

Off the top of my head, I would say that it does, because the jQuery paradigm is based on chaining together jQuery functions.

Comments

0

doc for closest

var matchingDiv = &(this).closest('.SomeClass')

if ( matchingDiv != null )
{
   //we found matching element now manipulate it

}

2 Comments

Sounds like he specifically wants a 'div' element with the class.
@patrick, I included the link of the documetacion of "closest", I think @VictorS can view the documentation and complete what you need
0

The difference between closest() and parents() is that parents() can give you more than one element if your selector matches more than one. (for what that's worth here)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.