getElementsByName() method


getElementsByName() Method


Definition and usage


getElementsByName() method can return the name with the specified name A collection of objects.

Syntax

document.getElementsByName(name)

##Parameters DescriptionRequired. The name of the element.
name


Browser support

QQ截图20161108165429.png##All major browsers support the getElementsByName() method

Instance

Instance

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script>
function getElements(){
	var x=document.getElementsByName("x");
	alert(x.length);
}
</script>
</head>
<body>

猫:
<input name="x" type="radio" value="猫">
狗:
<input name="x" type="radio" value="狗">
<input type="button" onclick="getElements()" value="多少名称为 'x'的元素?">

</body>
</html>

Run Instance»
Click the "Run Instance" button View online examples