0

I'm trying to reset a form with a confirm window. My html looks like:

<input type="reset" value="Reset" onclick="showReset();"/>

then for my javascript:

function doReset() {
    var formElements = document1.form1.elements;
    for (var i = 0; i < formElements.length; i++) {
        formElements[i].value = "";
        if (formElements[i].checked) {
            formElements[i].checked = false;
        }
    }
}

function showReset() {
    if (window.confirm("Do you really want to reset the form?")) {
        doReset();
    }
}

When I hit cancel on the confirm window, the form still gets reset and I'm not sure why. Thanks.

3 Answers 3

2

If I understand the question, the point is to bypass the default behavior and use your function instead. You can prevent the default button action by adding a return false to the showReset() function.

function showReset(e) {
    if (window.confirm("Do you really want to reset the form?")) {
        doReset();
    }
    return false;
}

Also, make sure the onclick handler returns the results of showReset:

<input type="reset" value="Reset" onclick="return showReset();"/>

Alternatively, if you want default browser behavior and just want a confirm dialog, why not use a confirm only? Like so:

<input type="reset" value="Reset" onclick="return window.confirm('Do you really want to reset the form?');"/>

This is just a "what to do", but if you want to get more info, there's some good info in these threads:

Sign up to request clarification or add additional context in comments.

Comments

2

It still gets reset because your input type is reset. Make that button or something and just use the form's reset method, like this:

document.form1.reset();

MDN Docs

Comments

0

Another way of doing it :

var r=confirm("Do you really want to reset the form?");  
if(r==true){  
//Reset logic  
}else{  
//Dont reset logic  
}

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.