bootstrap modal closes when ok on alert() is clicked

Issue

Bootstrap modal is closing when I click on “Ok” button showing from “alert()” function.

$("#expample").modal({
    backdrop: 'static',
    keyboard: false,
    show: true
});

Below is my jquery:

$('.sendToUser').on('click', function (){
            var selectedLyrics = $('#selectedLyricsInput').val();
            if($("input[name='selectUserRadio']").is(":checked") == false){
                alert('Please, select a user to send.');
                return;
            }
        });

Although, hitting “Enter” or “Esc” on keyboard is working fine and also, clicking outside the modal; the modal is not closing which is fine.

Below is my html:

<div class="modal fade" id="userSelectionModal" tabindex="-1" role="dialog" aria-labelledby="userSelectionModal">
    <div class="modal-dialog modal-likes" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                <h4 class="modal-title">Select User</h4>
            </div>

            <div class="modal-body">
                <div style="margin-bottom: 50px;" id="populateUsers">

                </div>
                <input type="hidden" id="selectedLyricsInput" value="" />
            </div>

            <div class="modal-footer">
                <button type="button" class="btn btn-success sendToUser" data-dismiss="modal">Send</button>
                <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
            </div>
            {{--<i class="fa fa-spinner fa-spin"></i>--}}
        </div>
    </div>
</div>

I am creating an alert popup from an event on the modal but when clicked on the “ok” of the alert() popup, it disappears.

How to prevent closing of the modal after clicking on “OK” on the alert popup?

Thanks

Solution

Your modal is closing, because you have data-dismiss="modal" attribute on the .sendToUser button. Removing this attribute should prevent the modal from closing.

Answered By – Stanislav Kvitash

Answer Checked By – Gilberto Lyons (AngularFixing Admin)

Leave a Reply

Your email address will not be published.