views:

268

answers:

2

I have this in my html code:

<ul id="navlist"
<li id="tabItem1"><a href="#">Item one</a></li>
<li id="tabItem2"><a href="#">Item two</a></li>
<li id="tabItem3"><a href="#">Item three</a></li>
<li id="tabItem4"><a href="#">Item four</a></li>
</ul>

I want to add a class to the a element with jquery like this: (its not working)

var currentTab = 1;

$(document).ready(function()
{        
  $("li#tabItem" + currentTab + ":has(a)").addClass("navlistHover");
  //console.log("li#tabItem" + currentTab + ":has(a)");
});

When a delete the a element and do it like the next example, than there is no problem.

$("li#tabItem" + currentTab).addClass("navlistHover");
+2  A: 

Try this:

$('navlist li#tabItem' + currentTab + ' a').addClass('navlistHover');

The :has() selector selects items that have the a element as children:

http://docs.jquery.com/Selectors/has

Will Vousden
That'll add the navListHover to the anchor tag, not to the LI which is what he is looking to do.
Parrots
That's not how I interpreted the question.
Will Vousden
He does say: 'I want to add a class to the a element'
tomlog
Apologies, I misread the question.
Parrots
A: 

I've tried the following code:

$('navlist li#tabItem' + currentTab + ' a').addClass('navlistHover');

but it's not working, very strange

lander
`$('#navlist li#tabItem' + currentTab + ' a').addClass('navlistHover');`
Balon
+1 good catch on the #navlist
Vinodh Ramasubramanian