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.
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.
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"); }); } });
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' .
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.
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
Try to do:
if (filters.length <= 0) {
$("#myModal").modal("hide");
}
//insert this code in your condition.
//for example.
if(...){
$('#myModal').modal('show');
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With