Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"click" listener gets execute multiple time

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 ?

like image 434
Saurabh Bayani Avatar asked Apr 17 '13 07:04

Saurabh Bayani


People also ask

Why click event triggering multiple times?

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.

How do you make an event listener only work once?

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.

What is Click () method?

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.

Is click an event listener?

The addEventListener() and onclick both listen for an event. Both can execute a callback function when a button is clicked.


2 Answers

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);       
    });
}
like image 184
Adil Avatar answered Oct 04 '22 05:10

Adil


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.

like image 22
tuvokki Avatar answered Oct 04 '22 06:10

tuvokki