Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 880
  • Last Modified:

jQuery return value to JavaScript

I am replacing the JavaScript standard confirm() dialog with a jQuery dialog.  I have no experience with using jQuery.

An excerpt of my code before the jQuery is this
      var confirmed = true;

      if(errors != "")
      {
         errors = warn1 + errors + "</ul>" + warn2;
         confirmed = confirm(errors);
         return confirmed;
      }
      return confirmed;

Open in new window


confirm gets the boolean value when the user clicks on OK or Cancel.
Then confirmed is returned and either the page submits (OK) or the user remains on the page (Cancel).

Now I'm substituting confirm() with the following:

         jConfirm(errors, 'Registration Errors', function(r) {
            jAlert('Confirmed: ' + r, 'Confirmation Results');
         });

Open in new window


I found the jConfirm dialog online at:
http://www.abeautifulsite.net/blog/2008/12/jquery-alert-dialogs/

There is an example of the dialog at:
http://labs.abeautifulsite.net/archived/jquery-alerts/demo/
(see 2nd button "Show Confirm")

With the jQuery dialog, I do not want the 2nd dialog to popup when the user clicks OK or Cancel.  Instead, I want the true/false value to be returned to the JavaScript confirmed variable and then the rest should require no change.

If there is another way to have the same behavior (submit when OK, remain on page if Cancel), I'll take that as well.  I want the 1st dialog with the "errors" message to remain until the user clicks OK or Cancel.  I don't want the 2nd dialog (jAlert) to popup.
0
mock5c
Asked:
mock5c
  • 2
  • 2
1 Solution
 
Gurvinder Pal SinghCommented:
you simply have to remove jAlert() call in the success handler

jConfirm(errors, 'Registration Errors', function(r) {
    if (r)
    {
            //invoke the 'ok' handler
    }
    else
    {
            //invoke the 'cancel' handler
    }
});
0
 
mock5cAuthor Commented:
Sorry, but as I said, I have no experience with jQuery.  Could you please provide an example of invoking the OK handler?

I've declared var confirmed outside the callback so wouldn't something like

jConfirm(errors, 'Registration Errors', function(r) {
confirmed = r;
});
0
 
Gurvinder Pal SinghCommented:
jquery solution is not a synchronous one, which will hold the execution of sequence of statements unless 'Ok' or 'Cancel' is clicked. So, this is why jConfirm uses callback handlers.

when i say 'invoke ok handler', i meant that you will invoke that code which you would have otherwise invoke when 'ok' would have been selected in the original javascript confirm box
0
 
mock5cAuthor Commented:
I've been trying to create a little test with the following.  I can't get jConfirm to popup at all.  In fact, if I do a console.log("here") after the $('.form').on('submit', function() line, I don't see that in the console output.  

I am hoping this little example is on the way to doing what I described above,

<script>
$(function() {
$('.form').on('submit', function() {
    var $form = $this;
    var $err = "errors";
   jConfirm( err, 'Errors', function(r) {

    if (r) {
       $form('submit').off().submit();
    }
    else {
      return false;
    }
  });

  return false;
});
});
</script>

Open in new window

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now