4

Having trouble with a piece of code to change both the background color and text color when a link is clicked.....

<div id="main_nav">
    <ul class="navigation">
        <li class="tab1"><a href="javascript:void(0);">My Account</a></li>
        <li class="tab1"><a href="javascript:void(0);">Available Times</a></li>
        <li class="tab1"><a href="javascript:void(0);">Completed Jobs</a></li>
        <li class="tab1"><a href="javascript:void(0);">New Jobs [<span class="menu_count2"></span>]</a></li>
        <li class="tab1"><a href="javascript:void(0);">Todays Jobs [<span class="menu_count"></span>]</a></li>
    </ul>
</div>

This is the jquery....

$(document).on('click', '.tab1', function(){
    $('.tab1').css({'background-color' : '#5B1762'});
    $('.tab1 a').css({'color' : '#fff'});
    $(this, '.tab1').css({'background-color': '#ccc'});
    $(this, '.tab1 a').css({'color': 'red'});
});

This changes the background color but the text remains white as in the css file.

1
  • What exactly is the problem, what is happening and what do you expect to happen ? Commented Nov 12, 2012 at 8:52

2 Answers 2

8

You are coding $(context, selector) instead of the $(selector, context), change:

$(this, '.tab1').css({'background-color': '#ccc'});
$(this, '.tab1 a').css({'color': 'red'});

To:

$(this).css({'background-color': '#ccc'});
$('a', this).css({'color': 'red'});
Sign up to request clarification or add additional context in comments.

Comments

2

Could be done that way too:

$('.tab1').click( function(){
    $('.tab1').css({'background-color' : '#5B1762'}).find('a').css({'color' : '#fff'});
    $(this).css({'background-color': '#ccc'}).find('a').css({'color': 'red'});
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.