Javascript DOM HTML Document getElementsByName() Method

Introduction

Get all elements with the specified name:

var x = document.getElementsByName("fname");

Click the button to get the tag name of the first element in the document that has a name attribute with the value "fname".

View in separate window

<!DOCTYPE html>
<html>
<body>

First Name: <input name="fname" type="text" value="Michael"><br>
First Name: <input name="fname" type="text" value="Doug">

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

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

<script>
function myFunction() {/*from w w  w .  j a  va 2  s.c o  m*/
  var x = document.getElementsByName("fname")[0].tagName;
  document.getElementById("demo").innerHTML = x;
}
</script>

</body>
</html>

The getElementsByName() method returns a collection of all elements with the specified name, as an HTMLCollection object.

The HTMLCollection object represents a collection of nodes.

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

document.getElementsByName(name)

Parameter Values

Parameter Type Description
name String Required. The name attribute value of the element to access



PreviousNext

Related