Question

I'm trying to do a simple: if (confirm()) { actions }, but some actions are done even if you didn't press confirm / cancel yet.

Here's my code:

$("#checkbox").click(function() {

    if (confirm("Continu ?")) {
        hideShowDiv("test");
        $(this).attr("checked", true);
    }
}

I this case, if you click on the checkbox, even if you didn't press confirm / cancel, the checkbox will be checked, but the div 'test' won't be displayed (unless you press confirm). I don't know why the line:

 $(this).attr("checked", true);

is executed before the return of confirm().

Was it helpful?

Solution

try add e.preventDefault();

$("#checkbox").click(function(e) {
    e.preventDefault();
    if (confirm("Continu ?")) {
        hideShowDiv("test");
        $(this).attr("checked", true);
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top