Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

jQuery return value to JavaScript

Posted on 2012-03-28
4
Medium Priority
?
819 Views
Last Modified: 2012-10-03
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
Comment
Question by:mock5c
  • 2
  • 2
4 Comments
 
LVL 40

Expert Comment

by:Gurvinder Pal Singh
ID: 37776955
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
 

Author Comment

by:mock5c
ID: 37777139
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
 
LVL 40

Accepted Solution

by:
Gurvinder Pal Singh earned 2000 total points
ID: 37780177
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
 

Author Comment

by:mock5c
ID: 37794378
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

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

OverviewThis article demonstrates a simple search form using AJAX. The purpose of the article is to demonstrate how to use the same code to render a page and javascript (JQuery) and AJAX to make subsequent calls to refine the results. The princip…
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

877 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question