How to bind events in jQuery
July 6, 2010 Category: jQuery TipsI 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
Tags: javascript, jQuery —

