Reputation: 3323
I have a modal component that has a button with data-dismiss="modal"
. I also need to bind another action when clicking this button. I tried:
$("#lsfModal").on("click", ".btn-ok", function(){
window.location = someURL;
});
I also tried with mousedown
instead of click
but with no luck. I noticed that this function isn't called at all.
Any ideas? Thanks
The html I'm using:
<div class="modal" id="lsfModal" tabindex="-1" role="dialog" aria-labelledby="lsfModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">×</span><span class="sr-only">Close</span></button>
<h4 class="modal-title" id="lsfModalLabel">Modal title</h4>
</div>
<div class="modal-body">
Lorem ipsum dolor sit amet
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default btn-cancel" data-dismiss="modal">Cancel</button>
<button type="button" class="btn btn-default btn-ok" data-dismiss="modal">OK</button>
</div>
</div>
</div>
</div>
LATER EDIT: Damn it. I was actually using a different selector: $("#lsfModal.confirm")
. The class confirm
was added later via javascript so that's why it was not working. :|
Upvotes: 0
Views: 9665
Reputation: 311
if u want to attach a handler to dynamically generated controls please use delegate syntax
$( "container" ).on( "click", "#urcontrol", function() {
alert( $( this ).text() );
});
Upvotes: 0
Reputation: 15861
I think you are adding model runtime. you need to use the delegation for this purpose.
$("body").on("click", "#lsfModal.btn-ok", function(){
// do some thing herer
});
Markup..
<button class="btn btn-primary btn-lg" data-toggle="modal" data-target="#myModal">
Launch demo modal
</button>
<!-- Modal -->
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">×</span><span class="sr-only">Close</span></button>
<button type="button " class="btn btn-primary deleteButton" ><span aria-hidden="true">×</span><span class="sr-only">Delete</span></button>
<h4 class="modal-title" id="myModalLabel">Modal title</h4>
</div>
<div class="modal-body">
...
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary">Save changes</button>
</div>
</div>
</div>
</div>
Javascript :
$("body").on("click", ".deleteButton", function(){
// do some thing herer
alert('sds');
});
You can play with bootply BootstrapDialog
Upvotes: 3