HTML | DOM Form length Property

The DOM Form length Property is used to return the number of input field contained in the form.

Syntax:

formObject.length

Return Value: It returns a numeric value which represent the the number of input field or elements in the Form.

Example-1: Return the number of input field.

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
  
<body style="text-align:center;">
    <h1 style="color:green;">
      GeeksForGeeks
  </h1>
    
    <h2>DOM Form length Property.
  </h2>
    
    <form id="users" 
          action="#">
        First name:
        
        <input type="text" 
               name="fname"
               value="Manas">
        <br> Last name:
        
        <input type="text"
               name="lname" 
               value="Chhabra">
        <br>
        
        <input type="submit" 
               value="Submit">
    </form>
  
    <p>Click the "Try it" button to return 
      the number of elements contained in the form.</p>
  
    <button onclick="myGeeks()">
      Try it
  </button>
  
    <p id="sudo" 
       style="font-size:25px;color:green;">
  </p>
  
    <script>
        function myGeeks() {
            
            // Return the number of input field
            var x =
                document.getElementById(
                  "users").length;
            
            document.getElementById("sudo").innerHTML = 
              "There are " + x + 
              " Input Field contained by the form.";
        }
    </script>
  
</body>
  
</html>

chevron_right


Output :

Before Clicking On Button :

After Clicking On Button :

Example-2: Return the value of input field.

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
  
<body style="text-align:center;">
    <h1 style="color:green;">
      GeeksForGeeks
  </h1>
    
    <h2>DOM Form length Property.</h2>
    <form id="users" 
          action="#">
        First name:
        
        <input type="text" 
               name="fname" 
               value="Manas">
        <br> Last name:
        
        <input type="text"
               name="lname" 
               value="Chhabra">
        <br>
        
        <input type="submit"
               value="Submit">
    </form>
  
    <p>Click the "Try it" button to return
      the value of each input field or element 
      contained in the form.</p>
  
    <button onclick="myGeeks()">Try it</button>
  
    <p id="sudo" style="font-size:25px;color:green;">
  </p>
  
    <script>
        function myGeeks() {
            
            //  Return values of input field.
            var x = document.getElementById("users");
            var text = "";
            var i = 0;
            for (i = 0; i < x.length; i++) {
                text = text + x.elements[i].value + "<br>";
            }
            document.getElementById("sudo").innerHTML = text;
        }
    </script>
  
</body>
  
</html>

chevron_right


Output:

Before clicking On Button:

After Clicking On Button:

Supported Browsers: The browser supported by DOM Form length Property are listed below:

  • Google Chrome
  • Internet Explorer
  • Firefox
  • Opera
  • Safari


My Personal Notes arrow_drop_up


Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.