0

I have an id #navigation li a and a class .menu-description . I want to change class from .menu-description to .menu-descriptionnew whenever user hovers on #navigation li a

My jquery so far:

<script>
$(document).ready(function() {
  $('#navigation li a').onmouseover(function() {

  //Check if element with class exists, if so change it to new
           if ($('div.menu-description').length != 0)
                $('div.menu-description').removeClass('menu-description').addClass('menu-descriptionnew');
           //Check if element with class 'menu-descriptionnew' exists, if so change it to 'menu-description'
           else if ($('div.menu-descriptionnew').length != 0)
                $('div.menu-descriptionnew').removeClass('menu-descriptionnew').addClass('menu-description');
                });
});
</script>

But it's not working. Any suggestions? Thanks!

1 Answer 1

2

The jQuery event is "mouseover" not "onmouseover"

You could clean up your code a lot with the use of .toggleClass() method and the .hover() event

Here's a simple example

Sign up to request clarification or add additional context in comments.

3 Comments

It works like this jsfiddle.net/PXU2w/3 But if I put "li a" everywhere near #navigation the background disappears and it stops working. Can you suggest any tips how to avoid this? Thanks.
The background disappears? The link that you sent me doesn't have that affect in it
are you sure you want the class to change when you hover over the link? you may want it to switch classes when you hover over the list item

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.