0

I have a problem with updating html element with javascript code. I'm trying to update html text input on google chrome console. For ex. cpuboss . com here is some html code from cpuboss

<input placeholder="find a cpu" name="query" type="text" id="product-lookup" autocomplete="off" class="">

I tried almost all code examples i found on stackoverflow but i couldn't update the text element.

document.getElementsByName('query').value = "test"

I tried .value .innerhtml .attribute etc. etc. but they doesn't work. Can someone help me why these codes not working ?

sword1st
  • 525
  • 3
  • 7
  • 21

1 Answers1

3

document.getElementsByName returns a NodeList, basically an array. You're gonna want the first one, like so:

document.getElementsByName('query')[0].value = "test";
vityavv
  • 1,482
  • 12
  • 23
  • 1
    You should add to suggest using the id since id must be unique, and he can just reference it direct: `document.getElementById('product-lookup').value = "test";` – IncredibleHat Mar 05 '18 at 21:57
  • 1
    @zevee I didn't know that. Thank you soo much it's worked. IncredibleHat Thanks for the tip! Some elements doesn't have id value so i will use it when available. – sword1st Mar 05 '18 at 22:00