0

I am trying to change the property of background color on mouseover of the nav ul li element but when I select another element of the same kind I want the first one to revert its color and the newly selected to change color and so on.

$(document).ready(function() { 
    $('nav ul li').on('mouseover', function() {
        if ($(this).css('background-color') == '#BBB') {
            $(this).css("background-color", "#36D900"); 
        } else {
            $(this).css("background-color", "#BBB");
        }   
    });
});
5
  • 9
    Okay, you have this code. What is your problem? Commented Apr 17, 2015 at 7:32
  • What is your question? Commented Apr 17, 2015 at 7:33
  • 1
    Use class instead of use css background color in jquery. The color is different dependent on browsers Commented Apr 17, 2015 at 7:34
  • Use hover event in jqyery Commented Apr 17, 2015 at 7:35
  • sry, aded an explanation now hope you can help. Commented Apr 17, 2015 at 7:36

1 Answer 1

1

Try the code

$('nav ul li').hover(function(){
   $('nav ul li').css("background-color", "#BBB");
   $(this).css("background-color", "#36D900"); 
});
Sign up to request clarification or add additional context in comments.

1 Comment

This seems to work perfectly. I don't know why mine doesn't work but this seems to do the job. Jquery seems to be a mess but I guess JS is a bigger mess :).

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.