How to attach multiple events at once to an element in jQuery?

To attach multiple events to an element, write the events separated by space.

function bindAction() {
alert("I am active !");
$("#btnBindAction").click(function () {
$("#btnAction").bind('mouseenter click', bindAction).val("Mouse hover Now and then click");
$("#btnUnbindAction").click(function () {
$("#btnAction").unbind('mouseenter click', bindAction).val("Simple button");

In the above code, when you click on the “btnBindAction” button, mouseenter as well as click event is attached with the first button. So when you try to mouse hover the 1st button you get the alert (now press Enter key to close the alert) and click on the button and you will again get alert.
To unbind the events, click on the “btnUnbindAction” button and both events will be unbound.

Enjoyed this post? Share it!


Leave a comment

Your email address will not be published.