change value of text using getElementsByName

URL87 picture URL87 · Aug 7, 2012 · Viewed 46.9k times · Source

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 ?

Answer

Matt Ball picture Matt Ball · Aug 7, 2012

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

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

and the rest will work.