document.getElementsByName()
method
The
getElementsByName() method accesses all elements with the specified name. this
method returns collection of elements that is an array.The
document.getElementsByName() method returns all the element of specified name.The
syntax of the getElementsByName() method is given below:The getElementsByName
method works differently in different browsers. In IE < 10,
getElementsByName() method will also return elements that have an id attribute
with the specified value. so you should be careful not to use the same string
as both a name and an ID.document.getElementsByName("name")
Example
<!DOCTYPE html>
<html>
<body>
First Name: <input name="fname" type="text" value="Nitin"><br>
First Name: <input name="lname" type="text" value="Kumar">
<p>Click the button to
get the tag name of the first element </p>
<button onclick="myFunction()">Click it</button>
<p id="output"></p>
<script>
function myFunction() {
var x = document.getElementsByName("fname")[0].tagName;
document.getElementById("output").innerHTML = x;
}
</script>
</body>
</html>
|