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 number of input field or elements in the Form.
Example-1: Return the number of input field.
<!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 > |
Output :
Before Clicking On Button :
After Clicking On Button :
Example-2: Return the value of input field.
<!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 > |
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
Recommended Posts:
- HTML | DOM Form name Property
- HTML | DOM Textarea form Property
- HTML | DOM Fieldset form Property
- HTML | DOM Form acceptCharset Property
- HTML | DOM Button form Property
- HTML | DOM Select form Property
- HTML | DOM Form autocomplete Property
- HTML | DOM form method Property
- HTML | DOM Form action Property
- HTML | DOM Form target Property
- HTML | DOM Form enctype Property
- HTML | DOM Input URL form Property
- HTML | DOM Form noValidate Property
- HTML | DOM Option form Property
- HTML | DOM Output form Property
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : Akanksha_Rai