First I tried a simple for loop. Didn't work. Then the google nightmare began. Somewhere it was suggested it must be done in reverse, from the last item to the first. Didnt' work either.
I've learnt that document.getElementsByClassName() doesn't return an array but a nodelist and the item() method was suggested. Didn't work either.
https://developer.mozilla.org/en-US/docs/Web/API/NodeList
Here's a simple example that's of course isn't working.
HTML
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Not worth it</title>
<script type="text/javascript">
<!--
function blah()
{
var idiots = document.getElementsByClassName("dumb");
for (var i = 0; i < idiots.length; i++) {
idiots.item(i).style.color = 'red';
}
document.onload = blah;
//-->
</script>
</head>
<body>
<p>
This is...<span class="dumb">dumb!</span></p>
<p>
This is...<span class="dumb">dumb!</span></p>
<p>
This is...<span class="dumb">dumb!</span></p>
</body>
</html>
<html>
<head>
<title>Not worth it</title>
<script type="text/javascript">
<!--
function blah()
{
var idiots = document.getElementsByClassName("dumb");
for (var i = 0; i < idiots.length; i++) {
idiots.item(i).style.color = 'red';
}
document.onload = blah;
//-->
</script>
</head>
<body>
<p>
This is...<span class="dumb">dumb!</span></p>
<p>
This is...<span class="dumb">dumb!</span></p>
<p>
This is...<span class="dumb">dumb!</span></p>
</body>
</html>
Help!