Peter
Peter

Reputation: 549

Jquery - Get parent id (parent must have a special Classname)

i have the "c_500" id and i need the id from next parent with the classname "pp".

i search something like ...

$('#c_500').getNextParentWithClass('pp');

html

 <div id="d_548" class="pp">

    <div class="class_ft">

      Blub<hr />
      <div id="here">content</div>
      <div id="c_500">content</div>

    </div>

  </div>

Thanks in advance! Peter

Upvotes: 2

Views: 2703

Answers (1)

Nick Craver
Nick Craver

Reputation: 630429

You can use .closest() with a .class selector to get the element, then just grab the ID, like this:

$('#c_500').closest('.pp').attr('id');

You can give it a try here, .closest() is the same behavior as .parents('pp:first'), walking up the parents until it finds the first one that matches the selector.

Upvotes: 6

Related Questions