How do I call another function when clicking the Modal button?

I want to use bootstrap modal on my page. At the same time when the modal trigger button is clicked I also want to do something in javascript. He does not work.

<button type="button" class="btn btn-primary center-block" id="start_quiz" data-toggle="modal" data-target="#myModal">
    Start Quiz
</button>

      

Button when clicked lauches modal but can't call:

$('#start_quiz').click(function(){
            alert("hello from javascript");  
        });

      

How can I achieve both results at the same time? Please help me.

+3


source to share


1 answer


Remove the attributes data-toggle="modal" data-target="#myModal"

from the button so it doesn't open the modal.

Then check if "hello from javascript" is displayed - it should do.

Then open the modal javascript code, add this line under your alert:

$('#myModal').modal('show')



Now you should make an alert and then show the modal. You can always flip these two if you want the modal images to appear first.

Second option , connect to the shown method and write your javascript there:

$('#myModal').on('shown.bs.modal', function () {
  // alert here
})

      

+3


source







All Articles