jQuery Traversing Tips

Just a quick review of tips available in jQuery to manage nodes (contents) near by the content / node which is triggered an event like click, hover etc

Functions: find, parent, parents, children, first, last, closest

Suppose you need to manage some nodes nearby a link or a button on an event.

Say

$(‘.button’).click(function(){

// code goes here

})

To get the current parent of triggered element, use $(this).parent()

To get the parent which has a class , use:

$(this).parents(‘.classname’);

To find any other element just above the node, use :

$(this).parents(‘.classname’).find(‘li’);

to get the first element in find, use

find(‘li:first’)

and for last, use

find(‘li:last’)

To get exactly the child nodes, use:

$(‘.classname’).children(‘some_selector_here’);

Here comes another very useful function, which is, closest

$(this).closest(‘td’)

Permanent link to this article: https://blog.openshell.in/2011/02/jquery-traversing-tips/

Leave a Reply

Your email address will not be published.