Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 946
sqlman Avatar asked Jan 08 '12 00:01

sqlman


2 Answers

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.

like image 177
yycroman Avatar answered Oct 06 '22 23:10

yycroman


This way probably:

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

Or:

$(this).find('li.li-top a:first')
like image 31
kaz Avatar answered Oct 07 '22 01:10

kaz