0
Is it possible to prevent a modal from appearing if a certain condition is reached? Example of what I want, more or less:
<button id="abreModal" class="btn btn-info btn-lg" data-toggle="modal" data-target="#modal">Open Modal</button>
//aí no js...
$('#abreModal').click(function () {
if(condicao){
// ... previne modal de abrir
}
});
I know that if I take the data-toggle and the data-target and show the modal manually, I can. But I have a lot of buttons like that, and I wouldn’t want to have to trade them all.
For that, I would have to remove all the
data-toggle
anddata-target
of the buttons and show the modal manually, as I said in the question, I have several buttons, and I did not want to make this change at all. I wonder if there is a way to simply prevent the modal from being shown– Artur Trapp
How do you open the modal?
– Wictor Chaves
I do not make any JS code to open. Only having the attributes in the buttons the modal opens alone with the click.
– Artur Trapp
If your buttons are with btn class and you put your condition and ruturn false will work
– Wictor Chaves
No, the
return false;
is not enough, I’ve tested– Artur Trapp
I did this example and it worked see: https://jsfiddle.net/bnLy8uvf/
– Wictor Chaves
Interestingly, it works, but I switched the handler to
$(document).delegate
and stopped working, see: https://jsfiddle.net/bnLy8uvf/1/– Artur Trapp
"As of jQuery 3.0, . delegate() has been deprecated" this function is deprecated for version 3, and I am using version 3 in this example, remembering that, you cannot use the same id on different elements so you must use for multiple buttons a class.
– Wictor Chaves
Yes, yes, this was just an example, but you’re right
– Artur Trapp