views:

4147

answers:

3

Hello everyone.

I need to display confirmation dialog on image click. Code in "onclick" section of image should be only executed if jquery confirmation dialog returned true.

At this point when user clicks on $(".submit_image") - dialog is displayed, but form is still submitted.

The logic should be the same for both jquery.alerts plugin or regular javascript confirm dialog.

<form name="myform">
<img class="submit_image" onclick="myform.field1.value='1';myform.field1.value='2'; myform.submit();">
</form>


$(document).ready(function(){
   // catch submit 
   $(".submit_image").click(function(e){
       jConfirm('Can you confirm this?', 'Confirmation Dialog', function(r) {
      return r;
       });
   });
});
A: 
return jConfirm('Can you confirm this?', 'Confirmation Dialog', function(r) {
  return r;
});

Maybe? jQuery needs you to return "false" from the .click function to stop the default action, and right now you're not returning anything from that function.

* EDIT *

I just looked at jConfirm, and because of how it works (specifically because it doesn't return anything; it can only return the value via its callback) it's impossible to use it the way you want. What you'll need to do instead is just ALWAYS return false from your onclick handler, and then in your jConfirm callback manually invoke a form submission.

Example:

$(document).ready(function(){
  // catch submit 
  $(".submit_image").click(function(e){
    jConfirm('Can you confirm this?', 'Confirmation Dialog', function(r) {
      // If they confirmed, manually trigger a form submission
      if (r) $(".submit_image").parents("FORM").submit();
    });
    // Always return false here since we don't know what jConfirm is going to do
    return false;
  });
});
machineghost
Thanks machineghost, but this doesnt work.
Kelvin
Well, my explanation remains correct, I just dunno how this crazy jConfirm thing works. What is function(r) returning to? You need to figure that out.Alternatively, you could just use a normal window.confirm dialog (which does return true/false based on what the user selected), but if you don't want to do that you need to read up on the documentation of your jConfirm function to figure out how to get a true/false back from it (and then return that true/false).
machineghost
Kelvin, please see my revised answer above and let me know if it worked for you.
machineghost
Thanks for reply machineghost. I tried this idea, but still no success. I think that "return false" do not prevent form from being submitted, since its executed in click event.
Kelvin
That can't be the issue. Try the following if you don't believe me; as you'll see the "false" returned from the image's onClick prevents the form's onSubmit from occurring:<html><head><script></script><body><form onSubmit="alert('test')"> <input type="image" src="http://sstatic.net/so/img/logo.png" onclick="return false"/></form></body></html>
machineghost
If you reduce your code to nothing but the following, does the form still submit? Also, do you have any other event handlers hooked up to the same elements?$(document).ready(function(){ // catch submit $(".submit_image").click(function(e){ return false });});
machineghost
What you doing in this example is a little different from what I have. I cant modify existing onclick event directly in image tag, by changing it to return=false. This is what I have: <!-- html --> <form onSubmit="alert('test')"> <input type="image" class="testbtn" src="sstatic.net/so/img/logo.png"; onclick="submit();"/> </form><!-- js --> $(".testbtn").click(function(e){ return false; });
Kelvin
Ok, now we're getting somewhere; what you just posted is significantly different than the code in your original question. In the above example, just remove the onclick="submit()" part; you don't need it, because your callback from JConfirm will call submit() (if appropriate.
machineghost
(If you use the sample code I provided in this answer I mean)
machineghost
hello machineghost, thanks a lot for your help. please see my answer below.
Kelvin
+1  A: 

You need to break up your events. One for the image's onclick to call form's onsubmit, and then form's onsubmit, which is the appropriate place for the 'false' return to halt form submission.

Jeff Ober
Jeff, shouldn't preventing the image's onClick prevent the form's onsubmit from ever occurring (via the stopPropagation call that jQuery will make if you return false)?
machineghost
A: 

I found solution to my problem, but some changes in existing code still needs to be performed.

In html I placed image inside of anchor tag with href = to what I had in image onclick event.

<form name="myform">
<a href="javascript: myform.field1.value='1';myform.field2.value='2'; myform.submit();" class="submit_image_link"><img src="..."></a>
</form>

Added e.preventDefault() method to prevent default event for anchor, which is following link or javascript in href section. As I understand, "return=false" or preventDefault() didn't work for me in my original code, because "img" tag doesnt have any default action.

$(document).ready(function(){
   // catch submit 
   $(".submit_image_link").click(function(e){
    e.preventDefault();
    jConfirm('Can you confirm this?', 'Confirmation Dialog', function(r) {
        if (r) location.href = $(".submit_image_link").attr('href');
    });
   });
});
Kelvin
Ok, three issues .... 1) Instead of using an a wrapped around an IMG and using JS to do the submit, there's a much simpler way: just use an <input type="image"/>; it even has the submit logic built right in ... 2) Returning false from a jQuery-connected event handler is IDENTICAL to calling e.preventDefault(); e.stopPropagation(); from that handler ... see http://docs.jquery.com/Events/bind ... 3) if someone wastes their time helping answer your question, it's rather rude to create your own answer and deny them any credit for all the time they spent.
machineghost
4) if you are going to use an A/IMG (instead of an INPUT[type='image']) you do not need myform.submit in your A's href, nor do you need the e.preventDefault; that's what causes all the problems. All you need is a submit call in your JConfirm callback. It's the difference between saying "submit whenever this is clicked; wait, you clicked? don't submit; you confirmed? ok now submit" and saying "you confirmed? submit". With your e.preventDefault you are ALWAYS preventing the default action on the A from happening; if you're going to do that, you might as well not hook it up in the first place.
machineghost
Hello machineghost, sorry if I offended you in any way, I definitively didn't mean to. I will repeat one more time that I appreciate your help. I cannot vote for your post yet, since my profile reputation is not high enough. Now, regarding your other corrections: 1) I didnt use <input type="image"/> since I didnt want to modify existing code a lot (also, this button is located outside of form). 2) Yes this is correct, I agree with you. 4) I kept original code, so other part of old application will not affected. But I agree with you that this would be better way. Thank you again for your help.
Kelvin
"I cannot vote for your post yet" My apologies, I forgot that some people couldn't approve answers (and I've had people do the "take my answer and then approve their own answer" thing before, so I jumped the gun). Glad I could be of assitance.
machineghost