Javascript Form How to - Use the name property to access a form object








Question

We would like to know how to use the name property to access a form object.

Answer


    <html>
    <body>
    <script language = "JavaScript">
<!-- ww  w . j  a v a2s  .  c o  m-->
    function checkName(){
         var firstName = document.formEx.first.value;
         var lastName = document.formEx.last.value;
         console.log("The name you entered is: " + firstName + " " + lastName);
    }
    </script>
    <form name="formEx">
    First Name:
    <input type="text" name="first" size="20">
    Last Name:
    <input type="text" name="last" size="25">
    <br><br>
    Click the button to check that the information is correct.
    <br>
    <input type="button" value="verify" name="check" onClick='checkName()'>
    </form>
    </body>
    </html>

The code above is rendered as follows: