Solved

jQuery return value to JavaScript

Posted on 2012-03-28
4
768 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 40

Expert Comment

by:gurvinder372
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:
gurvinder372 earned 500 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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…

734 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