104

I know can I select an element this way:

$("ul.topnav > li.target").css("border", "3px double red");

but how can I do something like:

$(this > li.target).css("border", "3px double red");

2 Answers 2

216
$( this ).find( 'li.target' ).css("border", "3px double red");

or

$( this ).children( 'li.target' ).css("border", "3px double red");

Use children for immediate descendants, or find for deeper elements.

2
  • 109
    Short-hand for find: $('li.target',this); Commented Feb 1, 2011 at 22:21
  • 2
    does find only return the first occurance or all the occurances?
    – Vignesh S
    Commented Nov 23, 2016 at 17:58
9

I use this to get the Parent, similarly for child

$( this ).children( 'li.target' ).css("border", "3px double red");

Good Luck

Not the answer you're looking for? Browse other questions tagged or ask your own question.