sqlman
sqlman

Reputation: 155

Selecting first anchor inside li element with JQuery

My HTML looks like this:

   <li class="li-top"><a class="top sub" href=#>Blah</a> .... </li>

What I am trying to do is to select the anchor tag, so I can change the color of the text ("Blah"). But here's the catch: I am using closest() because I am starting from a descendant of that li tag:

   $(this).closest('li.li-top');

How do I get that anchor tag from this starting point? I tried next(), each(), children(), etc. I can't get it. Thanks.

Upvotes: 11

Views: 43519

Answers (2)

yycroman
yycroman

Reputation: 7611

If you're starting from one of the children, you might try:

$(this).parents('li.li-top').find('a:first');

I often go this way to find 'cousins once removed' in the DOM.

Upvotes: 26

kaz
kaz

Reputation: 1943

This way probably:

$('li.li-top a:first')

Or:

$(this).find('li.li-top a:first')

Upvotes: 8

Related Questions