Find the closest div that has a specific class

Say I have this HTML code:

<div> <div class="required another_class"> <div class="some_class"> <input type="checkbox" name="some_check" value="" /> </div> </div> </div> 

How to find parent div of flag with class required ? As you can see above, the div has two classes required and another_class . So something like this:

 $(':checkbox').closest('div[class=required]'); 

Will not work. I thought I could do something like:

 $(':checkbox').closest('div').hasClass('required'); 

But that doesn't work either.

+7
javascript jquery
source share
2 answers

You can use the CSS selector in .closest() , like this:

 $(':checkbox').closest('div.required'); 
+13
source share

Alternatively you can also use

 $(':checkbox').parents('div.required') 
0
source share

All Articles