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
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.
Comments
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
Reidmere
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.