chicane
chicane

Reputation: 2071

Resetting a form with JavaScript

i am resetting my form with the following:

<script type="text/javascript">

window.onload = function () {
document.getElementsByName("email_address")[0].value = "";
document.getElementsByName("remove")[0].value = "off";
}



</script>

the problem is that the checkbox "remove" seems to get reset before the server side script can run. So in effect what ever the user selected on the checkbox is overridden and set to off before the php script can be run. Yet, the input box "email_address" is fine...

the test...

//this is already set to OFF by the time we get here....even if the user may have ticked the checkbox to ON
echo $HTTP_GET_VARS['remove'];

//this is fine, the email_address input is read and not reset...
echo $HTTP_GET_VARS['email_address'];

can any one explain this? I obviously want the form to be reset AFTER my server side script is done processing , yet for some reason it resets the checkbox early, yet the input box is ok...

thank you!

Upvotes: 1

Views: 285

Answers (3)

Josh Stodola
Josh Stodola

Reputation: 82483

Why not just...

document.forms[0].reset();

Upvotes: 1

Peter Bailey
Peter Bailey

Reputation: 105868

I'm really confused. You say you don't want to have the form cleared before it returns a post-submit value (are you doing an ajax form here?) yet the sample code you posted is set to execute when the page loads. Of course it's going to reset before a submit takes place.

Instead, you need to set it up to execute when the form submission is complete and you have the response you expect.

And don't bother with manually resetting each form element, just use the form.reset() method instead.

Upvotes: 3

perrohunter
perrohunter

Reputation: 3526

try with the following instead of off

document.getElementsByName("remove")[0].checked = false;

cheers

Upvotes: 1

Related Questions