Javascript Form How to - Access form element from elements array from form object








Question

We would like to know how to access form element from elements array from form object.

Answer


<!--  w  w w .j ava2s  .  c  o m-->
<html>
    <body>
    <script language="JavaScript">
    function getName(){
         var textName = document.form1.elements[0].name;
         console.log("The textbox name is: " + textName);
    }
    </script>
    <form name="form1">
    This is a blank input textbox. Click on the button below to get the name of the textbox.
    <br>
    <input type="text" name="textbox1" size=25>
    <br><br>
    <input type="button" value="Get Name" onClick = getName()>
    </form>
    </body>
</html>

The code above is rendered as follows: