Document getElementsByName() Method - Javascript DOM

Javascript examples for DOM:Document getElementsByName

Description

The getElementsByName() method returns elements for the specified name as a NodeList object.

The nodes in the NodeList can be accessed by index numbers. The index starts at 0.

Parameter Values

Parameter Type Description
name String Required. The name attribute value of the element you want to access/manipulate

Return Value:

A NodeList object. The elements in NodeList are sorted as they appear in the source code.

The following code shows how to Get all elements with the specified name:

Demo Code

ResultView the demo in separate window

<!DOCTYPE html>
<html>
<body>

First Name: <input name="fname" type="text" value="Mary"><br>
First Name: <input name="fname" type="text" value="Bond">

<button onclick="myFunction()">Test</button>

<p id="demo"></p>

<script>
function myFunction() {//from  w  w  w  .  j av  a2  s .c om
    var x = document.getElementsByName("fname")[0].tagName;
    document.getElementById("demo").innerHTML = x;
}
</script>

</body>
</html>

Related Tutorials