Solved

jQuery return value to JavaScript

Posted on 2012-03-28
4
727 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: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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
This article discusses how to create an extensible mechanism for linked drop downs.
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…

747 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now