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

JavascriptWeb DevelopmentFront End Scripts

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

Live Demo

<!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>
raja
Published on 26-Feb-2018 12:17:57
Advertisements