How to bind events in jQuery

Bookmark and Share
July 6, 2010 Category: jQuery Tips

I used to bind mouse events to dom elements using the .click() method like below:

$("a.button").click(function(){
	alert("A button was clicked");
})

The problem with this technique is that everytime a new button is added to the dom, you need to re-apply the click behavior to it.

Fortunately, jQuery introduced the .live() method in version 1.3 which solves that issue. The .live() method will bind an event to any element matching the selector now or at any point in the future, so if you you add new buttons to the page dynamically, they will also be binded to the event.

Use:as follows:

$('a.button').live('click', function(){
	alert("A button was clicked");
});

0 comment so far

Add New Comment




Your Comment