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

How to find the name of the first form in a document with JavaScript?


To get the name of the first form in a document, use the forms property with You can try to run the following code to display the name −

Example

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