Borut Flis
Borut Flis

Reputation: 16375

Using jQuery closest() method with class selector

$(this).closest(".fieldfilters");

This returns nothing for me. The HTML structure is like this:

<div class="fieldfilters" >
    <div class="filtri_ul_list">
        <ul>
            <li> <a></a></li>
        </ul>
    </div>
</div>

$(this) is the <a>. As far as I understand closest traverses the DOM up and finds the closest match. Is there a problem with the selector being a class? Why doesn't this work?

Upvotes: 2

Views: 9228

Answers (3)

marty
marty

Reputation: 4015

Your usage of .closest() is perfectly fine.

$(this).closest(".fieldfilters");

The most probable cause of your problem is that $(this) is not what you think it is. Check your context to see what 'this' really is.

Upvotes: 2

Greg
Greg

Reputation: 479

This will get you the first occurrence. $(".fieldfilters:first")

Upvotes: 0

Devang Rathod
Devang Rathod

Reputation: 6736

$(this).closest('div').find('.fieldfilters');

Upvotes: 0

Related Questions