Philip007 Philip007 - 2 months ago 12
Javascript Question

javascript getElementByName doesn't work

This simple js script can't set the value of "para". I guess getElementByName doesn't work. But why?

<script>
function fn()
{
document.getElementById("para").setAttribute("name","hi");
document.getElementByName("hi").setAttribute("value","my value is high");
}
</script>


HTML:

<input type="button" onClick="fn()" value="click me">
<input id="para" type="text" />

Answer

It's getElementsByName . Note the plural. It returns an array-like NodeList of elements with that name attribute.

Comments