1

I want to append a class to a div on hover of that div and remove that class when its not on hover. And when I hover on another div, again this class should be appended and removed when its not on hover. Someone say me how to do it in jquery......

2 Answers 2

3

You will have to use addClass and removeClass functions in jquery

Refer Following Code:

$(document).ready(function() {     
    $('#secID, #thID').hover(function(){     
        $('#oneID').addClass('red');    
    },     
    function(){    
        $('#oneID').removeClass('red');     
    });
});   

Can refer following links:

AddClass: http://api.jquery.com/addClass/

RemoveClass: http://api.jquery.com/removeClass/

Hope Its Helpful.

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

Comments

1

You can try toggleClass

$(function(){
    $('#id').hover(function(){
        $('#id2').toggleClass('classname');
    });
});

Or try this, because hover is no longer supported in jquery 1.9

$(function(){
    $('#id').on('mouseenter mouseleave', function(){
        $('#id2').toggleClass('classname');
    });
});

1 Comment

I would like to point out, even if it's a year later, that jQuery does support the hover element, for anyone looking this up. Still, the toggleClass is a nice idea.

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.