Search code examples
javascripthtmlformsgetelementsbytagname

change value of text using getElementsByName


I have firstName text that I want to change his value but it not work -

<input type="text" name="firstName" />
<script LANGUAGE="JavaScript" TYPE="text/javascript">
      document.getElementsByName("firstName").[0].value = "New Value " ;
</script>

How can I change this value ?


Solution

  • That's not syntactically valid JS. Remove the extra .:

    //                                    ↓↓
    document.getElementsByName("firstName")[0].value = "New Value " ;
    //                                    ↑↑
    

    and the rest will work.