How to find value of input field using name attribute?

Shyam Sundar picture Shyam Sundar · May 4, 2017 · Viewed 15.9k times · Source

How to access the value of this input field by its name attribute using Javascript

<input type='text' name='hey'>

document.querySelectorAll('input[name=hey]').value;

Answer

Satpal picture Satpal · May 4, 2017

You were close, As querySelectorAll() returns a list so you can use indexer to access the elements.

document.querySelectorAll('input[name=hey]')[0].value

better use querySelector()

document.querySelector('input[name=hey]').value