jQuery UI confirm

I was looking around earlier for a jQuery plugin to allow me to use jQuery UI’s dialog() widget to popup a confirm dialog. Didn’t have any luck finding one so I whipped something up. It’s not pretty but it works. You could even go as far as to overload window.confirm() but thats probably a bad idea.

jQuery.confirm = function(options){
	
	var opts = jQuery.extend( { message: "", ok: function(){}, cancel: function(){ } }, options );
	
	jQuery("<div class='span-10'><div class='ui-confirm-message'>" 
				+ opts.message + "<img class='loader' style='padding-left: 10px' src='/images/loader.gif' />" 
				+ "</div></div>").dialog({
					autoOpen: true,
					modal: true,
					autoOpen: false,
					resizable: false,
					draggable: false,
					title: "",
					width: "400px",
					buttons: {
					    "Cancel": function(){ 
							opts.cancel.call( this ); 
						},
						"Ok": function(){ 
					    	opts.ok.call( this );
					    }
					}
	}).dialog("open");
	
	
};

// use it 

		jQuery.confirm( {
			"message": "Are you sure?",
			"ok": function( ){
				
				$(this).find(".loader:first").show();
				
				// do stuff
                               $(this).dialog("close");
			},
			"cancel": function( ){ 
				$(this).dialog("close");
			}
		});

Questions or comments about this? Email us at contact@setfive.com.