I am trying to find the parent of an element and it\'s parent first child, the code is like this:
- &l
There is no need of total traversing back to the parent one after the other in a hierarchy, you can just do use parents()
instead of parent()
in this way
$('.selected').parents('ul.lowerMenu').children(:first-child').addClass('selected');
Try it this is working!!
If you go up two levels, then find the next child, you're only going down one level -- you're setting the selected
class on the <li>
, not the <a>
below it.. You need to go down another level. So it should be:
$('.selected').parent().parent().children(':first-child').children().addClass('selected');
Another way to get there is:
$('.selected').parent().siblings(':first-child').children().addClass('selected');
$('.selected').parent().siblings(':first-child').children().addClass('selected');
FIDDLE DEMO
$('.selected').closest('ul').find('li:first').addClass('selected');
reference closest