Umesh Patil Umesh Patil - 1 month ago 12
jQuery Question

Return value of jquery UI dialog Box

You may find solution over this in many posts(Post 1 , Post2 ), but their solution not working for me.

Here is the normal jquery dialog box written by me.

$("#dialog").dialog({
autoOpen:false,
buttons:{
"ok":function(){
$(this).dialog("close");
return true;
},
"cancel":function(){
$(this).dialog("close"); return false;
}
}
});


I will open the dialogbox with code:

var returnVal=$("#dialog").dialog("open");


I need to return
false
,if user clicks 'cancel' and return
true
if user clicks 'ok'.

var returnVal=$("#dialog").dialog("open");


I NEED
returnVal
to return
boolean
value(true/false), but it returns javascript
object
.

Answer

You cannot return something from the OK / cancel functions as they are essentially event handlers that are only processed upon the click of a button.

Use a separate function to process the result :

$mydialog = $("#dialog").dialog({
    autoOpen: false,
    buttons: {
        "ok": function() {
            $(this).dialog("close");
            processResult(true);
        },
        "cancel": function() {
            $(this).dialog("close");
            processResult(false);
        }
    }
});

$mydialog.dialog("open");


function processResult(result) {
    alert(result);
}

Working example : http://jsfiddle.net/nz2dH/

Comments