Posted by : Jebastin Sunday 27 October 2013

To find the first parent element using jQuery.

HTML:

  1. <div class="container1">
  2.    <div class="container2">
  3.       <div class="container3">
  4.          <div class="container4">
  5.              <div class="element">
  6.              ...
  7.          </div>
  8.       </div>
  9.    </div>
  10. </div>

Solution:

  1. $(".element").parents();
will give all parents of .element(including html and body)
DEMO

To find any specific parent, suppose container1 then
  1. $('.element').parents('.container1')
DEMO

jQuery .parents() generally find all parents, but if you passed a selector then it will search for that.

Leave a Reply

Subscribe to Posts | Subscribe to Comments

Link To This Post/Page

Spread The Word

Add this button to your blog:
JJ Technology Solutions

Blog Archive

Trackers

eXTReMe Tracker
facebook

- Copyright © JJ Technology Solutions - Powered by Source Code Solutions -