I have a <div>
, and I want to toggle its classes on hover.
Here is my code:
function a(){
this.classList.toggle('first');
this.classList.toggle('sec');
}
document.querySelector('#container').addEventListener('click', a );
I know there is no problem in my html or css. It is just that I have to change and put something in place of click
, but I don't know what.
Please help!!
While Tom Chung's approach definitely works, it's better to give
mouseenter
andmouseleave
their own handler :(see also this Fiddle)
Hover event is called "mouseenter" instead of "click".