Return the number of elements in a form:
var x = document.getElementById("myForm").length;
Click button to return the number of elements in myForm
.
<!DOCTYPE html> <html> <body> <form id="myForm" action="/action_page.php"> First name: <input type="text" name="fname" value="CSS"><br> Last name: <input type="text" name="lname" value="HTML"><br> <input type="submit" value="Submit"> </form>//from w ww . ja v a 2 s. com <button onclick="myFunction()">Test</button> <p id="demo"></p> <script> function myFunction() { var x = document.getElementById("myForm").length; document.getElementById("demo").innerHTML = x; } </script> </body> </html>
The length
property returns the number of elements in a form.
The length
property returns a number representing the number of elements in the form.