Friday, January 3, 2020

[jQuery] 2 handlers for hover Mouse Event



This source I want mention found from github and there are 2 handlers used as parameter in hover function (binded 2 handlers): The first paramters is handler when your mouse enter the element you selected, the second paramters is handler when your mouse leave the element you bind.
This is supported by .hover() and I checked but not works for .click(). This is the shorthand of "$(selector).mouseenter(handlerIn).mouseleave(handlerOut);"

Example:
<button id="btn1">Click</button>
<script>
$("#btn1").hover(function() {
  alert( "In" );
},function() {
  alert( "Out" );
});
</script>

Another method to write, with same result.
<button id="btn2">Click</button>
<script>
$("#btn2").mouseenter(function() {
  alert( "In" );
}).mouseleave(function() {
  alert( "Out" );
})
</script>

Reference:

https://api.jquery.com/hover/

No comments :

Post a Comment