JavaScript Confirm Modal using Bootstrap
August 21, 2012
Back in the olden days you might have added code like this to your form onsubmit, or an anchor to do a javascript confirmation box:
<a href="delete.cfm" onclick="return confirm('Are you sure you want to delete?');">Delete</a>
That works, but using onclick is not elegant and can lead to major issues when trying to implement things like Content-Security-Policy headers.
Looking for a better way to do this? Here's how you can do this leveraging the Bootstrap modal control:
First I add some JS to my document ready event handler:
$(document).ready(function() {
$('a[data-confirm]').click(function(ev) {
var href = $(this).attr('href');
if (!$('#dataConfirmModal').length) {
$('body').append('<div id="dataConfirmModal" class="modal" role="dialog" aria-labelledby="dataConfirmLabel" aria-hidden="true"><div class="modal-header"><button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button><h3 id="dataConfirmLabel">Please Confirm</h3></div><div class="modal-body"></div><div class="modal-footer"><button class="btn" data-dismiss="modal" aria-hidden="true">Cancel</button><a class="btn btn-primary" id="dataConfirmOK">OK</a></div></div>');
}
$('#dataConfirmModal').find('.modal-body').text($(this).attr('data-confirm'));
$('#dataConfirmOK').attr('href', href);
$('#dataConfirmModal').modal({show:true});
return false;
});
});
Now to trigger this in my HTML, I just add a data-confirm attribute to an anchor tag:
<a href="delete.cfm" data-confirm="Are you sure you want to delete?">Delete</a>
Tweet
Permalink | Add Comment |
add to del.icio.us
| Tags: javascript, bootstrap, twitter, modal, js, confirm
add to del.icio.us
| Tags: javascript, bootstrap, twitter, modal, js, confirm
Related Entries
- Getting Started with jQuery Mobile - October 29, 2010
- jQuery UI Autocomple IE 6 Select List z-Index Issues - August 16, 2010
- Using jQuery UI Autocomplete with Hidden ID's - July 14, 2010
- jQuery UI Sortable Tutorial - January 7, 2010
- Ext CFC - December 12, 2007
Trackbacks
Trackback Address: 809/A35B61202B735343C346DE983FCE4126
Comments
On 10/26/2012 at 8:12:20 AM EDT DrSumadhurMalhotra wrote:
1
Very Like this page java
On 03/12/2013 at 8:51:41 AM EDT Elvis wrote:
2
Perfect, works like a charm as a replacement for the onclick="return confirm(...
Do you could give me any advice how to use this method for my javascript confirms as well?
Example: if(confirm('blah blah'){ do this and that }
Greetings from Germany!
On 05/18/2013 at 4:42:20 AM EDT ejfmi1275 wrote:
3
mbt women shoeshttp://www.mbtshoesdiscount.co.uk mbt women shoes
On 05/21/2013 at 9:23:16 PM EDT xijxh6708 wrote:
4
mbt online shophttp://www.cheapmbtshoes2013.com/ mbt shoes sale
Post a Comment
Recent Entries
- Writing Secure CFML cfObjective 2013 Slides
- Upgrading to Java 7 on Linux
- J2EE Sessions in CF10 Uses Secure Cookies
- Learn about ColdFusion Security at cfObjective 2013
- Session Loss and Session Fixation in ColdFusion
- FuseGuard 2.3 Released
- CKEditor Spell Checker Plugin
- Adobe Says Go Ahead and Upgrade your ColdFusion JVM





