Javascript Form How to - Get how many form element created in the current page with Form length property








Question

We would like to know how to get how many form element created in the current page with Form length property.

Answer


<!--from  w ww  .  ja va 2s.c  om-->
<html>
<body>
<form name="form1" method="post" action="">
    <input type="text" name="textfield">
</form>
<form name="form2" method="post" action="">
    <input type="button" value="Only a sample button" onclick="return false"> 
</form>
<form name="form3" method="post" action="">
    <input type="checkbox" name="checkbox" value="checkbox">
</form>
<button onclick="function1()">Click here</button>
<script language="JavaScript">
function function1() {
    var m = document.forms.length;
    console.log("There are "+m+" forms in this document") 
} 
</script>
</body>
</html>

The code above is rendered as follows: