Click to See Complete Forum and Search --> : radio button to reset form
tiger66
07-02-2003, 06:31 PM
Hello
I am wondering is it possible to use the radio button to reset all the values in a form like click on the reset button? or I need to clear the field values one by one?
Thanks
<script type="text/javascript">
<!--
function clearValues(f){
for(i=0; i<f.elements.length-1; i++){
f.elements[i].value="";
}
}
//-->
</script></head>
<body>
<form action="" name="myForm"><div>
<input type="text" value="Clear me"><br>
<input type="text" value="Clear me, too!"><br>
<input type="radio" onclick="clearValues(this.form);"><br>
<input type="submit">
</div></form>
</body></html>
[J]ona
Charles
07-02-2003, 06:49 PM
1) That will clear the form and not reset it, though that might be what you really want.
2) As your radio control will only do something if JavaScript is enabled, you would do well do draw the control with JavaScript thereby avoiding some embarrassment.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta name="Content-Script-Type" content="text/javascript">
<title>Example</title>
<form action="">
<div>
<input type="text"><br>
<input type="text"><br>
<script type="text/javascript">
<!--
document.write('<input type="radio"><br>');
document.forms[0].elements[document.forms[0].elements.length-1].onclick = function () {this.form.reset()};
// -->
</script>
<input type="submit">
</div>
</form>
Always forget about that darned reset() method... :rolleyes:
Thanks, Charles. ;)
[J]ona
tiger66
07-07-2003, 09:37 AM
Hello
Does the reset() method only reset the entire form at once? Can it reset the tags one by one or reset by tag's name?
Thanks
The reset() method only resets all of the values in the form that it refers to. (e.g., this.form.reset(); or document.forms["formName"].reset(); ).
[J]ona