tags:

views:

35

answers:

1

hi, i'm trying to select the <a> within the <span> that's the nth child of the <li>

HTML

<li>
 <span><a></a></span>
 <span><a></a></span>
 ......
 <span><a></a></span>
</li>

jQuery

$(function(){
 if ($('body').attr('class') == 'special') {
  $('li span:nth-child(1) a').css({'color' : '#444444'});
 };
});

what am i missing here because it doesn't seem to select the <a>

thanks

+2  A: 

Your code works, you can test it here, make sure of a few things:

What your code is running in a document.ready handler, like this:

$(function() {
  $('li span:nth-child(1) a').css({'color' : '#444444'});
});

And that your anchors have an href or a name attribute, otherwise the browser may render them as disabled, ignoring some styling.

Nick Craver
i have this jQuery within an if statement. the <a> tag have the href attribut but no name attribute
pixeltocode
@pixeltocode - If you put before the same line `alert($('li span:nth-child(1) a').length);` what number do you get?
Nick Craver
i get 0 in the alert
pixeltocode
@pixeltocode - Are the elements being added dynamically? Also an `if()` statement doesn't say anything...if it running on `document.ready` like in my code above?
Nick Craver
i've edited the jQuery. please see edits, thanks
pixeltocode
@pixeltocode - Are the elements being added dynamically? AJAX, etc?
Nick Craver
@Nick - the elements are NOT being added dynamically.
pixeltocode
@pixeltocode - Do you have a link to the page? Something's different from your posted question...the code you have works, provided the `<body>` does have that class.
Nick Craver
@Nick - I've got two if statements, one that check the id of the body and another that checks the class. Does that affect it?
pixeltocode
@pixeltocode - yup, you need to ensure that the statement is even running first
Nick Craver
@Nick, i combined the if statements into one, it is running but the <a> is still not being selected.
pixeltocode
@pixeltocode - I can't help you debug this...if you have a link, then that works, but currently you're asking a question and not posting the *actual* code, it's really hard to find the problem when you don't post the info.
Nick Craver
@Nick - there was an extra <a> generated dynamically. I wasn't aware of that. So, instead of nth-child(1), I've had to use nth-child(2) and it works now. Thanks
pixeltocode