Axel Briche Axel Briche - 6 months ago 53
Javascript Question

Javascript inside a modal duplicated everytime I open the modal

I have a bootstrap modal with a form and javascript inside.

My modal content (called by ajax) :

<script src=""></script>
<button class="action">Submit</button>

$('.action').on('click', function(){

Everytime I click on my button, I have a lot of alert displayed (not only one), the javascript inside the modal is not reset/killed when I open and close the modal again and again.

Answer Source

I have solved my problem, I listen the click event not directly on the submit button (button inside the ajax return), but on the modal wrapper and I place my JS outside the modal :

$('#modal-wrapper').on('click', '#my-submit-btn', function () { ...

Now, the JS work and is not duplicated.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download