Can't check if modal is open

Viewed 204

I can't check if the modal is open or not. Below is my code.

<button id="btnOpenModal" class="btn btn-primary lift" data-toggle="modal" data-target="#myModal">
    Open Modal 
</button>

<!-- Modal -->
<div class="modal fade fixed-right" id="myModal" tabindex="-1" aria-modal="true" role="dialog">
    <div class="modal-dialog modal-dialog-vertical d-none" role="document">
        <div class="modal-content bg-glass">...</div>
    </div>
</div>

This is my JQuery.

$(document).ready(function() {
    if ($("#btnOpenModal").on("click", function(e) {
        if($('#myModal').hasClass("show")) console.log("shown");
    }));
});

The modal is dynamically added show class into its class when the button is clicked, and I want to check that show class is there.

1 Answers

Remove the if statement that you wrapped around the modal button onclick event, like so:

   $("#btnOpenModal").on("click", function(e) { 
   setTimeout(()=>{
   if($('#myModal').hasClass("show"))     {console.log("shown");}
   }, 2000)
   });

It will wait for 2 seconds and then perform the check

Related