Click to See Complete Forum and Search --> : form help, please!
Hello! I have this problem: I have a form and at some point I need to reset it (i need a blank form) so i tried this:
<SCRIPT LANGUAGE="JavaScript">
<!--
function erase(form1)
{
for (var el = 0; el <form1.elements.length; el++)
{
form1.elements[el].value="";
}
this.reset();
}
<!- end script ->
</SCRIPT>
....
<INPUT TYPE="button" VALUE="Erase" onClick="erase(this.form)">
It's not working. Do i have to set in each field defaultValue to null? I would apreciate any help.
Khalid Ali
06-16-2003, 01:31 AM
Why would you do a thing like that???
while you can use an html element called
<input type="reset" value="Reset">
Just add the above line in your form and you are all set
thank you, but i already tried that.Reset only works until i submit the form.i need to clear the form at any time.
Gollum
06-16-2003, 02:16 AM
You mean you are modifying form elements after the form has been submitted? Hmmm, could lead to unpredictable results.
Why are you trying to do this?
The form reads from a database an ID element. If ID exists, it displays that record for the user to modify it. After the modification i want to clear the form because any unwanted change would show in the database.
Maybe the attached file would help.
Gollum
06-16-2003, 02:47 AM
Wouldn't it be better to just wait for the next page from the server?
However, if you really must clear the form elements for presentation purposes, you could try using hidden elements...
<form name=theForm onsubmit="HideStuff(this);">
<input type=text name=Address>
<input type=hidden name=hAddress>
</form>
then
function HideStuff(form1)
{
for (var el = 0; el <form1.elements.length; el++)
{
var oElt = form1.elements[el];
if ( oElt.type == "text" )
{
form1.elements['h' + oElt.name].value = oElt.value;
}
}
}
Then your server will access all the hidden fields for the form values instead of the visible ones.
Khalid Ali
06-16-2003, 07:15 AM
if you are using a server side pangauge,then I am sure its your server side script thats reloading the submitted pages info because if you submit a form all its datta should be lost...anyways my point is once a form a submitted don't re-generate it from server side... thats the simplest solution...if you can't use reset button...which I still am surprised why won't work...
yes,my script is reloading the page so if i want to clear the form it should be done in my script, not in js. Thank you, anyway.