Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 452
Sathish Avatar asked Mar 14 '14 05:03

Sathish


People also ask

How do I make bootstrap modal not close on click outside?

When the Button is clicked, the HTML DIV is referenced using jQuery and its modal function is called along with properties data-backdrop: "static" and data-keyboard: false which disables the closing of the Bootstrap Modal Popup when clicked outside.

How do I open modal on condition?

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"); }); } });

How do I hide the modal when I click outside?

closest(". modal") , then call the closeModal() function. When the closeModal() function is called, it selects the . modal class selector and hides it with display = 'none' .

How do I open modal popup on modal popup?

To trigger the modal window, you need to use a button or a link. Then include the two data-* attributes: data-toggle="modal" opens the modal window. data-target="#myModal" points to the id of the modal.


3 Answers

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

like image 138
Andrew Brown Avatar answered Oct 19 '22 02:10

Andrew Brown


Try to do:

if (filters.length <= 0) {
    $("#myModal").modal("hide");
}
like image 7
Felix Avatar answered Oct 19 '22 01:10

Felix


//insert this code in your condition.
//for example.
    
if(...){
    $('#myModal').modal('show');
 }
like image 1
Sarfraz Rasheed Avatar answered Oct 19 '22 02:10

Sarfraz Rasheed