jQuery .click () event, does not work with tag button (with Bootstrap 3)

0

I have a website with 2 files (index.php and modals.php). In index.php I use include("modals.php"); and in modals.php I have several modals that are generated dynamically, according to the information in DB.

In modals.php I have 2 buttons (one created using the <button>Abre</button> tag and another using the <input type"button" value="Abre" /> tag.

My goal is to use the <button></button> tag to trigger the event, since I'm using Bootstrap 3 and I want the button to have no text but a glyphicon.

In index.php I have:

<script type="text/javascript">
$( document ).ready(function() {
    $(".botaoDoModal").click(function () {
        alert("Funciona!");
    });

});
</script>

But this script only works when I add class="botaoDoModal" to the <input> tag, the same does not happen with the <button></button> tag.

I do not know why, or if I was wrong and somehow ...

    
asked by anonymous 07.06.2014 / 20:33

2 answers

0

I do not know what the error was, but I copied another button that had somewhere in the code and pasted it, I added the class and it worked, it must have been a syntax error

    
07.06.2014 / 21:44
1

Always prefer to use the .on() event instead of click() . Because you can have dynamically generated elements (for example coming from an AJAX call), you may want to have the same click handler that was previously bound to the same element selector by "delegating" the click event using on () as an argument to other dynamically created elements.

Following is a very good article from the Tableless site about .on () and .off (): link

    
08.06.2014 / 15:10