5

I have a bunch of validation javascript processes with confirms. I want to use jquery ui dialog, but I need to return true for the rest of the validation processes.

For example:

var where_to_coupon = confirm(pm_info_msg_013);
      if (where_to_coupon== true) {
      doSubmit=true;
      return doSubmit;

So I need a function to replace confirm with a UI dialog, pull the message string (pm_info_msg_013), and use my own buttons or UI buttons to return true for the validation process.

Not sure where to start with this one.

help?

3 Answers 3

10

In jQuery UI dialog, set the modal option to true, and specify primary and secondary user actions with the buttons option.

    $( "#dialog-confirm" ).dialog({
        resizable: false,
        height:140,
        modal: true,
        buttons: [{
            text: pm_info_msg_013,
            click : function() {    
                $( this ).dialog( "close" );
                // code related to "where_to_coupon== true" goes here
                // submit form
                }
            }, {
            text: "Cancel",
            click: function() {
                $( this ).dialog( "close" );
                doSubmit = false;
                // don't submit form
            }}]
    });

See the demo here: http://jqueryui.com/demos/dialog/#modal-confirmation

Update: This will allow you to create multiple confirms. Usage:

function CreateDialog(okText, cancelText, okCallback, cancelCallback) {
        $( "#dialog-confirm" ).dialog({
            resizable: false,
            height:140,
            modal: true,
            buttons: [{
                text: okText,
                click : function() {    
                    $( this ).dialog( "close" );
                    okCallback();
                    }
                }, {
                text: cancelText,
                click: function() {
                    $( this ).dialog( "close" );
                    cancelCallback();
                }}]
            }
        });

// ******* usage #1 ********    
CreateDialog(pm_info_msg_013, "Cancel", function () {
   // where_to_coupon== true
}, function() {

   // where_to_coupon== false
});

function OnConfirmTrue() {
  // do something
}

function OnConfirmFalse() {
  // do something
}

// ******* usage #2 ********

CreateDialog(pm_info_msg_013, "Cancel", OnConfirmTrue, OnConfirmFalse);
Sign up to request clarification or add additional context in comments.

8 Comments

not sure how to incorporate with my javascript confirm string
You would need to dynamically change the html of the dialog-confirm div ($("#dialog-confirm").html(pm_info_msg_013)) before calling the above code
ok, thanks for that. Next question being how to trigger a "if (where_to_coupon== true) {"
@Jason: Put them inside the respective click functions. You can call other functions on your page from there too. You don't need to check for where_to_coupon== true as the handlers get called based on what button was clicked. See my update again.
but doesn't that only return the text: pm_info_msg_013, versus the string the pm_info_msg_013 references?
|
1

I have created a plugin for jQuery UI, called dialogWrapper, that provides several methods, including a confirm method. You call it like this:

$.confirm("Prompt", function(){}, function(){}, {});

The second and third arguments are what to do when the yes and no button are clicked, respectively. The fourth is for additional arguments.

Check it out here: http://code.google.com/p/dialogwrapper/

It doesn't work like the native JavaScript confirm method, though, since it is asynchronous. So you wouldn't be able to directly overwrite confirm with $.confirm.

5 Comments

looks interesting, but I'm trying to touch minimal code (especially in the javascript) - so how would I return true or false within your plugin?
You can't, since $.confirm works asynchronously. It relies entirely on callbacks. There is no way to use jQuery Dialogs synchronously without ugly, UI-blocking code (for example, a while loop that checks if a button has been pressed yet).
well, I assume there's gotta be a way (a second function) or an onclick event to trigger a true?
what if I used my own buttons within the error message, instead of using jquery UI's buttons?
I'm not quite sure what you mean
0

Have you tried Impromtu by Trent Richardson?

http://trentrichardson.com/Impromptu/

This jquery plug-in can give you the control you are looking for with prompts and confirms and more.

1 Comment

Please, try to read this stackoverflow.com/help/deleted-answers, to get more understanding how to not answer. Namely: "Answers that do not fundamentally answer the question": barely more than a link to an external site

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.