Another nodelist that you can retrieve using JavaScript is the one containing all of the tags in the page that have a specific name attribute. Unlike the id attribute the name attribute is not required to be unique and so the getElementsByName returns a nodelist rather than a specific node.
This particular nodelist is not all that useful since the only place a modern web page will use the name attribute is within a form to provide the server with field names for the fields being submitted from a form and in most cases those names will be both unique and will correspond to the id on the same tag that is there to allow the label to be attached.
There are only two situations where the names of the form fields need to be different from their ids - one is with radio buttons where all the buttons in a group need to have the same name and the other is where you are setting up a number of similar form fields to be passed to the server as an array. In the latter case the names will have [] on the end and you will need to include that as a part of the name when referencing those fields using their name from within JavaScript.
This particular nodelist is not all that useful since the only place a modern web page will use the name attribute is within a form to provide the server with field names for the fields being submitted from a form and in most cases those names will be both unique and will correspond to the id on the same tag that is there to allow the label to be attached.
There are only two situations where the names of the form fields need to be different from their ids - one is with radio buttons where all the buttons in a group need to have the same name and the other is where you are setting up a number of similar form fields to be passed to the server as an array. In the latter case the names will have [] on the end and you will need to include that as a part of the name when referencing those fields using their name from within JavaScript.
HTML
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<head>
<title>Example D03</title>
</head>
<body>
<form action="#">
<div>
<input type="radio" id="r1" name="size" value="s">
<label for="r1">Small</label>
<input type="radio" id="r2" name="size" value="m">
<label for="r2">Medium</label>
<input type="radio" id="r3" name="size" value="l">
<label for="r3">Large</label>
</div>
</form>
<script type="text/javascript" src="exampleD03.js"></script>
</body>
</html>
JavaScript
var node; node = document.getElementsByName('size');
for (var i = node.length-1; i>=0; i--) {
node[i].style.background-color = '#000';
}
SHARE