jQuery: How to get to a particular child of a parent? jQuery: How to get to a particular child of a parent? jquery jquery

jQuery: How to get to a particular child of a parent?


Calling .parents(".box .something1") will return all parent elements that match the selector .box .something. In other words, it will return parent elements that are .something1 and are inside of .box.

You need to get the children of the closest parent, like this:

$(this).closest('.box').children('.something1')

This code calls .closest to get the innermost parent matching a selector, then calls .children on that parent element to find the uncle you're looking for.


$(this).parent()

Tree traversal is fun

$(this).parent().siblings(".something1");$(this).parent().prev(); // if you always want the parent's previous sibling$(this).parents(".box").children(".something1");

And much more ways, you might find these docs helpful.


This will find the first parent with class box then find the first child class with regex matching something and get the id.

$(".mylink").closest(".box").find('[class*="something"]').first().attr("id")