0

I have a button & I want to add bootstrap confirmation this is working fine when I'm calling directly but When I'm using confirmation code inside onClick Event this then Bootsrap Confirmation is not working. What is wrong here?

This is my code:-

$('.myButton').on("click", function() {
  $('[data-toggle=confirmation]').confirmation({
    rootSelector: '[data-toggle=confirmation]'
});
}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>

<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-confirmation2/4.2.1/bootstrap-confirmation.min.js" integrity="sha512-LhgCuBuvISlfR3iVmjgchM6JrYvl16h/RJVsjdCfGXUahMHgmMJpavyIbluTuV3Ww8tJtGh2VAYayfoGvc84DA==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>

<button data-toggle="confirmation" type="button" class="myButton mt-5 btn btn-primary btn-convert">Convert</button>

ThankYou!

Rohit Verma
  • 3,657
  • 7
  • 37
  • 75
  • are you sure about the selector? because that button doesn't have that class. – Vivek Choudhary Sep 22 '21 at 12:24
  • yes I'm sure this is working fine if i'm using without click. – Rohit Verma Sep 22 '21 at 12:25
  • @RohitVerma I think VPC is referring to the `$(".myButton")` selector - there is no button with that class – Nick Parsons Sep 22 '21 at 12:28
  • @Nick Parsons right now I have updated but still not working... – Rohit Verma Sep 22 '21 at 12:29
  • If you fix the syntax error by adding a `);` at the end of your code then it's working as expected. The `confirmation()` method doesn't cause the confirmation pop-up, it creates a listener on the element that you specified to perform the confirmation when it's clicked. So with the current code, when you first click it won't appear, but you'll be adding the click event handler for the confirmation, then on the second click, the confirmation will appear due to the confirmation-event listener being added via the previous click. – Nick Parsons Sep 22 '21 at 12:32

0 Answers0