In JavaScript and VBScript, the getElementsByName is a function which is used to access all HTML elements with a specified name. Two different elements can be declared with same name unlike their Id which must be defined unique. Tag getElementsByName can be used for usage related problems of getElementsByName function.
Wiki:
The getElementsByName()
is a JavaScript and VBScript function which can be used in web pages to accesses html elements with their specified name.
The getElementsByName
method is supported in all major browsers.
The function getElementsByName() can be used to access more than one element because html
elements can be defined with same name unlike theirIDs
which should be defined unique.
Syntax usage:
document.getElementsByName('name')
Example:
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">
function getElements()
{
var x=document.getElementsByName("x");
alert(x.length);
}
</script>
</head>
<body>
Cats:
<input name="x" type="radio" value="Cats">
Dogs:
<input name="x" type="radio" value="Dogs">
<input type="button" onclick="getElements()" value="Elements with name 'x'?">
</body>
</html>
Tag usage:
The tag getelementsbyname can be used for programming problems related to usage of getElementsByName
function. Tag getelementsbyname should be avoided for theoretical and opinion based questions.
Read more: