Javascript Form How to - Reset the fields in a form by calling the reset() function








Question

We would like to know how to reset the fields in a form by calling the reset() function.

Answer


<html>
<head>
<script type="text/javascript">
function formReset(){<!--   w  ww. j a  v  a 2  s.com-->
    var x=document.forms.myForm
    x.reset()
}
</script>
</head>
<body>
<form name="myForm">
<p>Enter some text in the text fields and then press the "Reset form" button</p>
<input type="text" size="20"><br>
<input type="text" size="20"><br>
<br>
<input type="button" onclick="formReset()" value="Reset form">
</form>

</body>
</html>

The code above is rendered as follows: