Matheus Miranda Matheus Miranda -4 years ago 136
Javascript Question

Event type of shown.bs.modal calling 2 times

Please check my code:

HTML:

<button type="button" class="btn btn-info btn-lg" data-toggle="modal" data-target="#myModal">Teste</button>

<!-- Modal -->
<div id="myModal" class="modal fade" role="dialog">
<div class="modal-dialog">

<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">&times;</button>
<h4 class="modal-title">Teste modal</h4>
</div>
<div class="modal-body">
<label class="control-label">Select File</label>
<input id="input-1a" type="file" class="file" data-show-preview="false">
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Fechar</button>
</div>
</div>

</div>
</div>


JS:

$('#myModal').on('shown.bs.modal', function(e) {
alert("alert");
})


I'm using this plugin: http://plugins.krajee.com/file-input#usage

Here it is in jsfiddle: https://jsfiddle.net/DTcHh/30285/

In line
$('#myModal').on('shown.bs.modal', function(e)
, it does 2 times.

Removing line
<input id="input-1a" type="file" class="file" data-show-preview="false">
, works correctly.

Any solution ?

Answer Source

You are using two JavaScript libraries that can run into conflicts with jQuery. In order to avoid these conflicts, you need to put jQuery in no-conflict mode as following:

jQuery(document).ready(function( $ ) {
    $('#myModal').on('shown.bs.modal', function(e) {
       alert("alert");
       e.stopPropagation();
    })
});

Hope this help!

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download