views:

38

answers:

2

Hi Guys,

I have the following html

<div class="sub">
       <a href="#" id="people">People</a>
</div>

which has the CSS for the "a" as

color:#999999;
font-size:31px;

I am trying to use jQuery to get this to change the color using a class "active" which just has "color:#777!important;" but everytime I do this it just doesn't work. I am trying to do it via

$('.sub').click(function() {
                $('a#people').addClass('active');
            })

But it doesn't work ? Anyone know how I can add a class to an "a" element ? i.e. tried this but doesn't work

                $('.sub a').addClass('active');

Thanks

+2  A: 

What you've got there is correct: see it working here. If it's not working for you, then there's something else we don't know about which is affecting it.

nickf
I just love jsbin.
Erik
hmm thats weird haha. i thought i had it right - ok let me check! ill get back asap
Tom
the "active" class is actually per #sub .active - does this mean the "addClass" should be .addClass('#sub .active') ?
Tom
no; your adding the equiv. of `class='active'` to your element, you wouldn't put `class='sub active'` in your document.
Erik
yep solved )) thanks!
Tom
+1  A: 

What you have works just fine. You can see it at:

http://jsbin.com/oweye/edit

My guess is you didn't wrap the click assignment in a document ready like this:

$(document).ready(function() {

    $('.sub').click(function() {
      $('a#people').addClass('active');
    });
 });

If you didn't its tryingh to assign the handler before the element exists. I changed the colors in your CSS to make the effect more obvious.

Erik
lol @ the jsbin url. "ze goggles. they do nuthing!"
nickf