Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 ?

like image 812
URL87 Avatar asked Aug 07 '12 18:08

URL87


People also ask

What is the getElementsByName () method commonly used to obtain?

The getElementsByName() method of the Document object returns a NodeList Collection of elements with a given name attribute in the document.

How do you get getElementsByName value?

How it works: First, select the submit button by its id btnRate using the getElementById() method. Second, listen to the click event of the submit button. Third, get all the radio buttons using the getElementsByName() and show the selected value in the output element.

What is the difference between document GetElementByID and document getElementsByName?

The difference is that GetElementByID() will retrieve a single element object based on the unique id specified, whereas GetElementsByTagName() will retrieve an array of all element objects with the specified tag name. Then you can obtain the value using GetElementById from your C++ code.

Is there a getElementsByName?

The getElementsByName() method returns a collection of elements with a specified name. The getElementsByName() method returns a live NodeList.


1 Answers

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

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

and the rest will work.

like image 102
Matt Ball Avatar answered Oct 05 '22 22:10

Matt Ball