document.getElementsByName

getElementsByName() is a method in JavaScript through which we can accesses all elements with the specified name.
Example :

<html>
<head>
<script type=”text/javascript”>
function getbyNameDemo(){
var sub = document.getElementsByName(‘rbtnSubjects’);

var mySubjects = “”;
for(i=0; i<sub.length; i++) {
if(sub[i].checked) {
mySubjects += sub[i].value+”,”;
}
}
alert(“Your have choosed “+mySubjects);
}
</script>
</head>
<body>
choose your favorite subjects :
Computer <input type=’checkbox’ value=”Computer” name=”rbtnSubjects”/>
Music <input type=’checkbox’ value=”Music” name=”rbtnSubjects”/>
Cooking <input type=’checkbox’ value=”Cooking” name=”rbtnSubjects”/>
<input type=”button” onclick=”getbyNameDemo()” value=”Submit”/>
</body>
</html>

Leave a Reply