1

I try to add class to my div. How? Look bottom:

$(document).ready(function(){
    		$(".menu-div").hover(function(){
       		 $(this).addClass("menu-div-hover");
   	        });
});
.menu-div-hover{
     background-color:#25262A;
}

.menu-div{
          height:50px;
          border-top:;
          border-bottom:;
    background-color:yellow;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<a href=""><div class="text-center menu-div">FAQ</div></a>

It is my http://jsfiddle.net/pj4oqffu/ What I did wrong? I don't want answers, I need to learn it. If you can help me, please do it :)

1
  • It works , you need to replace 'menu-div-hover ' after 'menu-div' Commented Oct 11, 2015 at 12:28

3 Answers 3

4

Why use jQuery at all?

Simply use CSS and the :hover selector:

.menu-div:hover{
     background-color:#25262A;
}

If you need to do it via jQuery, simply move the hover class below the original (Your jQuery code is fine).

.menu-div
{
  height:50px;
  border-top:;
  border-bottom:;
  background-color:yellow;
}

.menu-div-hover
{
  background-color:#25262A;
}

Which will take precedence, you can also use !important on the hover background colour.

http://jsfiddle.net/pj4oqffu/1/

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

2 Comments

Nice it is it. Do you know how to do it with jQuery? Its working but I need to know the same in jQuery :)
your js code was ok, you just have to "override" the class backround property using !important jsfiddle.net/pj4oqffu/2
3

If you want to user $.hover then you need to set and remove the class:

 $(document).ready(function(){
     $(".menu-div").hover(
         function(){
             $(this).addClass("menu-div-hover");
         },
         function(){
             $(this).removeClass("menu-div-hover");
        });
 });

Also you need to define .menu-div-hover after .menu-div in order to overwrite the background-color during hover.

Comments

1

No sure why you are using jquery for that where you can simply do that using css. But to your problem the solution is you need to add important tag to your css as following:

.menu-div-hover{
     background-color:#25262A !important;
}

Here is the edit of your code example: http://jsfiddle.net/pj4oqffu/3/

But I strongly suggest you do that using plain css as following:

.menu-div:hover{
     background-color:#25262A !important;
}

Check the fiddle: http://jsfiddle.net/pj4oqffu/5/

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.