Javascript Form How to - Get number of elements within a form








Question

We would like to know how to get number of elements within a form.

Answer


   <!--from ww w .j a v a  2  s . c  om-->
<html>
    <body>
    <script language="JavaScript">
    function showNumElements(){
         console.log("There are " + document.form1.length + " elements in this document");
    }
    </script>
    <form name="form1">
        Enter First Name:
        <input type="text" size=15><br>
        Enter Last Name:
        <input type="text" size=20><br>
        Enter address:
        <input type="text" size=40><br><br>
        <input type="button" value="Submit" onClick=showNumElements()>
    </form>
    </body>
</html>

The code above is rendered as follows: