Jquery short hand for parents children

I continue to work on elements related to other elements, but my methods see a little lover!

those. to

//  <div> <div> <div> <ul> matched item where script is called from </ul> </div> </div> <a class="postMore">LINK</a> </div>

i use;

$(this).parent('div').parent('div').parent('div').children('a.postMore').css('display','block');

which seems a little bulky. Is there a better way to handle relative elements?

+3
source share
2 answers

There are several ways to traverse the DOM using jQuery , so you just need to identify the template in your html structure and use the most appropriate methods.

In your example, this should do what you want, and it provides much more flexibility.

$(this).closest('div:has(a.postMore)').find('a.postMore');

demo http://jsfiddle.net/gaby/j2Wgv/


+6

.closest(), DOM. , div, a.postMore , $(this).closest('class').children('a.postMore').

+2

All Articles