OK, I'm by no means a JavaScript guru, and perhaps I'm still thinking in terms of Desktop software development, but this is what I'm trying to achieve :
Question :
I mean :
This my HTML code for the modal (should the buttons be - somehow - acting via their onclick
javascript routines?) - also note that #confirmMessage
is going to be variable.
<div class="modal fade hide" id="confirmAlert">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">x</button>
<h3 id="confirmTitle">Are you sure?</h3>
</div>
<div class="modal-body">
<p id="confirmMessage">Body</p>
</div>
<div class="modal-footer">
<a href="#" class="btn" data-dismiss="modal">Cancel</a>
<a href="#" class="btn btn-danger">Yes</a>
</div>
</div>
Just an idea:
checkBeforeExec(message,functionToExec)
#confirmMessage
to message and Yes' href to javascript:functionToExec
I know it may sound a bit confusing - but I simply do not know what the most javascript-friendly way to do this would be...
I created a dialog manager around modal, with confirm
helper function that does essentially this:
var callback = function(result) {
alert(result);
}); // define your callback somewhere
$('#confirmAlert').on('click', '.btn, .close', function() {
$(this).addClass('modal-result'); // mark which button was clicked
}).on('hidden', function() {
var result = $(this).find('.modal-result').filter('.btn-danger').length > 0; // attempt to filter by what you consider the "YES" button; if it was clicked, result should be true.
callback(result); // invoke the callback with result
});
Also, you should make both the Cancel and Yes button data-dismiss="modal"
.
Here's a fiddle, with a simple example of my dialog manager.
Note, that if you are using Bootstrap 3, you should add a handler to the hidden.bs.modal
event instead of hidden
.
With jquery you can use something like
$('.btn').click(function(){
val=$(this).val()
if(val=='Yes'){
//continue the processing
}
})
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