I am trying to create the table which creates rows using ajax. the problem is that when i assigned the "click" listener based on the class name it is getting called multiple time
my code is
function fn_getAlertRules(parentRowId) {
$.ajax({
type: "GET",
url: anyURL,
contentType: "application/json",
dataType: "json"
}).done(function(data) {
// create row to add to parent table's row
var s_rulesHtmlString = '<tr><td colspan="3" class="rules-parent-tr"><table><tbody>';
$.each(data, function(i, name) {
s_rulesHtmlString += '<tr class="rule-tr">';
s_rulesHtmlString += '<td class="eventid-td">Rule ID:'+ name.RuleId+'<span>' + name.OccuredEventId + '</span></td>';
s_rulesHtmlString += '<td>' + name.Name + '</td><td>' + name.RuleDate + '</td>';
s_rulesHtmlString += '</tr>';
});
s_rulesHtmlString += '</tbody></table></td></tr>';
// add row below parent tr
$(parentRowId).parent().after(s_rulesHtmlString);
$(".rule-tr").on("click", "td", function(event) {
// this code blocks get executed multiple times
});
});
}
can anybody please tell me, why it is getting called multiple time ?
an Event will fire multiple time when it is registered multiple times (even if to the same handler). eg $("ctrl"). on('click', somefunction) if this piece of code is executed every time the page is partially refreshed, the event is being registered each time too.
We can pass an object as an argument to the addEventListener method and specify that the event is only handled once. This is achieved by passing the property once to the object. If we set once to true, the event will only be fired once.
click() method simulates a mouse click on an element. When click() is used with supported elements (such as an <input> ), it fires the element's click event. This event then bubbles up to elements higher in the document tree (or event chain) and fires their click events.
The addEventListener() and onclick both listen for an event. Both can execute a callback function when a button is clicked.
You are binding click
event in each (loop) and event is binded as many times as loop is executed, that is why you get repeated click events. You can delegate the click event to parent of elements to be added or document
before ajax call and event will be binded automatically to dynamically added element being added in done function. You can read more about event delegation using on here
Delegated events have the advantage that they can process events from descendant elements that are added to the document at a later time. By picking an element that is guaranteed to be present at the time the delegated event handler is attached, you can use delegated events to avoid the need to frequently attach and remove event handlers. Reference.
function fn_getAlertRules(parentRowId) {
$(document).on("click", ".rule-tr td", function(event) {
// this code blocks get executed multiple times
});
$.ajax({
type: "GET",
url: anyURL,
contentType: "application/json",
dataType: "json"
}).done(function(data) {
// create row to add to parent table's row
var s_rulesHtmlString = '<tr><td colspan="3" class="rules-parent-tr"><table><tbody>';
$.each(data, function(i, name) {
s_rulesHtmlString += '<tr class="rule-tr">';
s_rulesHtmlString += '<td class="eventid-td">Rule ID:'+ name.RuleId+'<span>' + name.OccuredEventId + '</span></td>';
s_rulesHtmlString += '<td>' + name.Name + '</td><td>' + name.RuleDate + '</td>';
s_rulesHtmlString += '</tr>';
});
s_rulesHtmlString += '</tbody></table></td></tr>';
// add row below parent tr
$(parentRowId).parent().after(s_rulesHtmlString);
});
}
That's because you have multiple table rows with the same class assigned to it. If you want to get it executed only for the item that is clicked, assign a unique id to it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With