jquery - Overriding Javascript Confirm() while preserving the callback -



point, want override standard js confirm() function within jquery plugin. have figured out how simple function layout below.

function confirm(opts) {    //code } 

now, want call function within confirm function above, so...

function confirm(opts) {    openmodal(opts); } 

the openmodal function open custom modal window message , required buttons. buttons both <span> id of either submit or cancel. submit returns true , cancel returns false. now, how return either true or false based on button clicked?

for example...

$('#openmodal').live('click', function() {    var opts = { title:'modal title', message:'this modal!' };    var resp = confirm(opts);    if(resp)        // user clicked <span id="submit"></span>    else        // user clicked <span id="cancel"></span> }); 

hopefully understand mean.

this is possible not describe. typical way confirm function used is:

if(confirm("are sure want submit form?")) {     form.submit(); } 

in above example, button press returned synchronously script. execution paused within function until button pressed.

a "modal dialog" overlay on entire contents of html web page. browser, buttons within such overlay work same way buttons anywhere else within page do.

since javascript handles such button clicks asynchronously (i.e. using callback functions handle events) rather synchronously, approach attempting not work.

you need change parts of code use confirm function this:

confirm("are sure want submit form?", function(result) {     if(result) {         form.submit();     } }); 

this work registering event handler jquery does. script execution continues immediately, skipping past code in anonymous function. later, browser call function indirectly through javascript event handler register within confirm function, purpose call callback function either true or false appropriate.


Comments

Popular posts from this blog

ASP.NET/SQL find the element ID and update database -

c++ - Compiling static TagLib 1.6.3 libraries for Windows -

PostgreSQL 9.x - pg_read_binary_file & inserting files into bytea -