How to reset or clear a form using JavaScript?

JavascriptWeb DevelopmentFront End Scripts

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.

Example

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

Live Demo

<!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>
raja
Published on 07-Feb-2018 07:40:03
Advertisements