0

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 !");
});
1
  • You're sure it isn't adding the event handler multiple times? Also, maybe if it's bubbling weird you could try passing the event to the click handler and adding event.stopPropagation() at the top to keep it from triggering on anything else up the DOM. Commented Apr 18, 2018 at 14:40

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.