0

I have the following code:

function hvcm_wait() {
  waitingDialog.show('Please wait while your VM is rebooting...');
}

$('#hv_ConfirmShutDown').on('show.bs.modal', function(e) {
  $(this).find('.btn-ok').attr('href', $(e.relatedTarget).data('href'));
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<a class="btn btn-danger btn-ok">Shutdown Now</a>

How do I call the javascript function hvcm_wait() when .btn-ok is clicked is clicked in Modal?

Any help would be highly apperciated. Thanks!

1
  • <a class="btn btn-danger btn-ok" onclick="hvcm_wait()">Shutdown Now</a> change your anchor tag with this. Commented Feb 20, 2018 at 7:54

4 Answers 4

1

Assuming that .btn-ok is not present when script is loaded, so use event-delegation.

Add these lines after hvcm_wait() function

$('#hv_ConfirmShutDown').on('click', '.btn-ok', function() {
    hvcm_wait();
})

Or if it is present in DOM then you can directly do this

$('.btn-ok').click(function() {
    hvcm_wait();
})
Sign up to request clarification or add additional context in comments.

Comments

1

Or you can add it straight to your HTML like this:

<a class="btn btn-danger btn-ok" onclick="hvcm_wait()">Shutdown Now</a>

Comments

0

I would use this, first in the $() you search for the class btn-ok, then you hand it a onClick handler that executes the function.

$(".btn-ok").click(function(){
    hvcm_wait();
});

1 Comment

It worked, thanks. I used: $( ".btn-ok" ).click(function() { $('#hv_ConfirmShutDown').modal('hide'); waitingDialog.show('Please wait...'); });
0

You are adding href attribute here. First you need to prevent the href and call the normal function.

 $(".btn-ok").click(function(event){
     event.preventDefault();
     hvcm_wait(); 
 });

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.