Add Click Function To Dynamically Created Html Tags
So I have a div that contains a number of other divs. Each of these other divs has a class called record. I only use this class to select all of these divs.
.live() in this case. This is a perfect situation for jQuery's
.delegate()
method, which is more efficient.
$("#resultContainer").delegate('.record','click',function() {
// do stuff here...
});
Only clicks inside the resultContainer
need to be processed to see if they match .record
, where .live()
will need to process every click on the page.
Solution 2:
Attach a handler to the event for all elements which match the current selector, now and in the future.
$(".record").live("click", function(e) {
//do stuff here...
});
Solution 3:
As of jQuery 1.7 you should use on()
instead of live()
or delegate()
. From the documentation:
As of jQuery 1.7, the .on() method provides all functionality required for attaching event handlers.
The on()
method should be attached to a static parent or document
:
$(document).on( 'click', '.record', function(e) {
// do stuff here....
});
Solution 4:
Take a look at the jQuery live() function
You can add an event listener for all div, regardless of changes in the page
Solution 5:
Try live() method. Instead of:
$(".record").click(function(e) {
do stuff here....
});
Try this:
$(".record").live(function(e) {
do stuff here....
});
Post a Comment for "Add Click Function To Dynamically Created Html Tags"