0

i've been trying to get a confirm box to work, i am using php and jquery to make a confirm box appear when clicking on a delete link, actual code :

$(document).ready(function(){
    if (jQuery("a.delete-link").length > 0) {
        $("a.delete-link").bind("click", function(){
            return confirm("Sunteti sigur ca doriti sa stergeti?");
        });
    }
});

and the link is called :

<a href="javascript:void(0);" class="formSubmit delete-link" id="sterge">sterge</a>

the link is used to submit a form when clicked, the code for that is :

$(document).ready(function(){
    if ($(".formSubmit").length > 0) {
        if ($(".formSubmit").parents("form").find("input:submit").length == 0) {
            $(".formSubmit").parents("form").append('<div style="width:1px;height:1px;overflow:hidden;"><input style="width:0;height:0;overflow:hidden;" type="submit" /></div>');
        }
        $(".formSubmit").click(function(){
            $(this).parents("form").trigger("submit");
            return false;
        });
    }
});

i do get the confirm dialog, but any option i chose, the form submits and the delete action is called.. any idea what i'm doing wrong ?

1
  • update : is there any way to make it work and still keep things in separate functions ? the answer Darin Dimitrov provided works best but the functions are not necessarily called together
    – Raz
    Dec 30, 2009 at 11:57

3 Answers 3

4

Bind the confirmation to the onSubmit of the form. You'll save a lot of hassle that way and you will get a confirmation no matter how the form was submited.

$( document ).ready ( function () {
    $( 'selector for your form' ).submit ( function () {
        return confirm ( 'Are you sure ...?' );
    } );
} );
2
  • thank you for your solution, but due to the nature of the framework i have to use, i can't do that
    – Raz
    Dec 30, 2009 at 11:07
  • in some parts of the website the function assigned for the confirm box is used independent of a form, that would be my main issue, also the code you provided seems to ask a confirmation each time a form is submitted, and i don't need that, i need the confirm only for a certain link class.
    – Raz
    Dec 30, 2009 at 12:02
1

You have two click events bound to the anchor tag. The first event shows the confirm and the second submits the form.

Trigger the form submission only if the user confirmed:

$(document).ready(function(){
    if ($(".formSubmit").length > 0) {
        if ($(".formSubmit").parents("form").find("input:submit").length == 0) {
            $(".formSubmit").parents("form").append('<div style="width:1px;height:1px;overflow:hidden;"><input style="width:0;height:0;overflow:hidden;" type="submit" /></div>');
        }
        $(".formSubmit").click(function(){
            if ($(this).hasClass('delete-link') && confirm("Sunteti sigur ca doriti sa stergeti?"))
            {
                $(this).parents("form").trigger("submit");
            }
            return false;
        });
    }
});
2
  • thank you for your answer, this seems the best way so far, i will test it and get back to you.
    – Raz
    Dec 30, 2009 at 11:09
  • indeed, this was the solution that helped the most, the only edit i did was : instead of the "if ($(this).hasClass('delete-link') && confirm("Sunteti sigur ca doriti sa stergeti?"))" block i used if ($(this).hasClass('delete-link-form')) { if (confirm("Sunteti sigur ca doriti sa stergeti?")){ $(this).parents("form").trigger("submit"); } else return false; }
    – Raz
    Jan 4, 2010 at 9:36
0

Can you use this:

<a href="#" onclick"return javascript:void(0);" ... />

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.