user1220569
user1220569

Reputation: 37

Using javascript to set value of input type text

i can't figure out how to set a value using javascript.

Here is how the HTML look like:

<input type=​"text" name=​"cardholderName" data-bind=​"value:​ creditCardForm.name, 
css:​    creditCardForm.style.name" maxlength=​"30">​

When i try to set the value in the Chrome console

document.getElementsByName('cardholderName').value="myname";

The console returns "myname" but the field in the browser isn't populated with this approach. Any hints on what is missing? Thanks

Upvotes: 0

Views: 59

Answers (1)

Abraham Hamidi
Abraham Hamidi

Reputation: 13789

getElementsByName returns an array, and there is no property value on an array. I think you mean to get the first item:

document.getElementsByName('cardholderName')[0].value="myname";

Upvotes: 2

Related Questions