Computer >> Computer tutorials >  >> Programming >> Javascript

How to reset or clear a form using JavaScript?


Reset or clear a form using JavaScript, with the reset() method. The reset method sets the values of all elements in a form like clicking the reset button. How to reset or clear a form using JavaScript?

Example

You can try to run the following code to reset a form using JavaScript −

<!DOCTYPE html>
<html>
   <head>
      <title>Reset HTML form</title>
   </head>
   <body>
      <form id="newForm">
         Student Name<br><input type="text" name="sname"><br>
         Student Subject<br><input type="password" name="ssubject"><br>
         <input type="button" onclick="newFunction()" value="Reset">
      </form>
      <script>
         function newFunction() {
            document.getElementById("newForm").reset();
         }
      </script>
   </body>
</html>