Axel Briche Axel Briche - 28 days ago 8
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="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button class="action">Submit</button>

<script>
$('.action').on('click', function(){
alert('ok');
});
</script>





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.