Sathish
Sathish

Reputation: 309

Bootstrap Modal open on condition

I'm using Bootstrap modal popup to show content in popup and I'm using if/else condition to open modal popup. I don't want to open modal popup when condition is false. My code:

<a data-toggle="modal" class="btn btn-primary" style="font-size: 10px" href="#" data-target="#myModal" title="Edit"><span class="glyphicon glyphicon-pencil"></span>Edit</a>

My jQuery is:

 $('a[data-target=#myModal]').on('click', function (ev) {
        ev.preventDefault();
        if (filters.length <= 0) {
            alert('Please select any one item in grid');
        }
        else {
            $(this).attr('href', '/GeoRegion/Edit/' + filters[0]);
            var target = $(this).attr("href");

            // load the url and show modal on success
            $("#myModal").load(target, function () {
                $("#myModal").modal("show");
            });
        }
    });

If filters.length<=0 then I don't want to open popup. Now popup opening with empty content.

Upvotes: 8

Views: 25715

Answers (3)

Sarfraz Rasheed
Sarfraz Rasheed

Reputation: 121

//insert this code in your condition.
//for example.
    
if(...){
    $('#myModal').modal('show');
 }

Upvotes: 1

Andrew Brown
Andrew Brown

Reputation: 5424

The problem is that you have data-toggle="modal" on your button, which is the data-attributes (HTML5) way of using modals. this will work without any javascript written.

remove data-toggle and then your javascript should run correctly.

documentation

Upvotes: 13

Felix
Felix

Reputation: 38102

Try to do:

if (filters.length <= 0) {
    $("#myModal").modal("hide");
}

Upvotes: 7

Related Questions