JavaScript get element by name
Note the plural in this method:
document.getElementsByName()
That returns an array of elements, so use [0] to get the first occurence, e.g.
document.getElementsByName()[0]
The reason you're seeing that error is because document.getElementsByName
returns a NodeList
of elements. And a NodeList
of elements does not have a .value
property.
Use this instead:
document.getElementsByName("acc")[0].value
You want this:
function validate() {
var acc = document.getElementsByName('acc')[0].value;
var pass = document.getElementsByName('pass')[0].value;
alert (acc);
}
All Answers here seem to be outdated. Please use this now:
document.querySelector("[name='acc']");
document.querySelector("[name='pass']")