$("#dataTable tbody").on("click", "tr", function(event){
alert($(this).text());
});
is this syntax is for tr's which are already on the Page ?
and if not , What is the syntax for future tr's elements ?
$("#dataTable tbody").on("click", "tr", function(event){
alert($(this).text());
});
is this syntax is for tr's which are already on the Page ?
and if not , What is the syntax for future tr's elements ?
For future elements inside #dataTable tbody
, your code should work. To delegate the click all the way up to the document, allowing the same handler for future <tr>
anywhere on the page, try:
$(document).on("click", "tr", function(event){
alert($(this).text());
});
Adding to Davids accepted answer, you can also use this solution to bind multiple events to a selector as well as all future matching elements.
For example an input element.
$(document).on({
focus:function(){
alert($(this).val());
},
blur: function(){
alert($(this).val());
}
}, 'input[type=text]');
This will handle all <tr>
s, no matter when they were created, but only within the currently-existing #dataTable tbody
.
It's equivalent to .delegate
.
You have to use jQuery'S .live() for future elements
Wrong, as this is jQuery 1.7 (and i was to fast in posting my answer).
.on()
works for all (future) elements.
From api.jquery.com/on/
The .on() method attaches event handlers to the currently selected set of elements in the jQuery object. As of jQuery 1.7, the .on() method provides all functionality required for attaching event handlers. For help in converting from older jQuery event methods, see .bind(), .delegate(), and .live(). To remove events bound with .on(), see .off(). To attach an event that runs only once and then removes itself, see .one()
and from api.jquery.com/live/
As of jQuery 1.7, the .live() method is deprecated. Use .on() to attach event handlers. Users of older versions of jQuery should use .delegate() in preference to .live().