Bisvan
Bisvan

Reputation: 127

Click events firing multiple times on button generated dynamically

I generate with jquery, one dynamic div content. When I click on #change_alert I display an alert. If I click second time on #change_alert, the alert is displaying 2 times. If I click third time on #change_alert, the alert is displaying 3 times. Do you have an idea to help me and have juste one alert (even if I click more) ?

Thank you

var dynamic_content = '<div id="change">'+
           '<div class="view">'+
           '<a href="#" id="change_alert">CLICK</a>'+
            '</div></div>';



$$(document).on('click', '#change_alert', function () {
 alert("TEST !");
});

Upvotes: 0

Views: 54

Answers (1)

STJ
STJ

Reputation: 51

I have tried this code and it works:

$(document).ready(function() {
  var dynamic_content = '<div id="change">'+
        '<div class="view">'+
       '<a href="#" id="change_alert">CLICK</a>'+
        '</div></div>';

  $('#content').html(dynamic_content);

  $(document).on('click', '#change_alert', function () {
    alert("TEST !");
  });
});

Maybe you are creating more than one dynamic button.

Upvotes: 1

Related Questions