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

How to get the number of forms in a document with JavaScript?


To get the number of forms in a document, use the form property with length property in JavaScript.

Example

You can try to run the following code to get the number of forms in a document −

<!DOCTYPE html>
<html>
   <body>
      <form>
         Name: <input type="text" name="my_name" value="Amit">
         Subject: <input type="text" name="my_subject" value="Java">
         <input type="submit" value="Submit">
      </form>
      <script>
         var val = document.forms.length;
         document.write("<br>Number of forms in the document: "+val);
      </script>
   </body>
</html>