I'm performing a small text with JavaScript with the getElementsByClassName()
and I am getting some unwanted results. I would like the script to change each CSS class to a new class. The issue is that every other class is only changing...
I would like to use pure js how this issue as it is for practice purposes.
The first thing that came to mind was white spaces, although when removing the this did not make any differnce.
Can anyone point our what I am doing wrong?
<html>
<head>
<link rel="stylesheet" type="text/css" href="default.css">
</head>
<body>
<div class="block-default">BLOCK1</div>
<div class="block-default">BLOCK2</div>
<div class="block-default">BLOCK3</div>
<div class="block-default">BLOCK4</div>
<div class="block-default">BLOCK5</div>
<div class="block-default">BLOCK6</div>
<div class="block-default">BLOCK7</div>
<div class="block-default">BLOCK8</div>
<script>
var blockSet = document.getElementsByClassName("block-default");
var blockSetLength = blockSet.length;
blockSet[0].className = "block-selected";
blockSet[1].className = "block-selected";
blockSet[2].className = "block-selected";
blockSet[3].className = "block-selected";
blockSet[4].className = "block-selected";
blockSet[5].className = "block-selected";
blockSet[6].className = "block-selected";
blockSet[7].className = "block-selected";
</script>
</body>
</html>
CSS Classes:
.block-default {
width: 100px;
height:50px;
background-color: green;
border: 1px solid red;
padding:10px;
}
.block-selected {
width: 100px;
height:50px;
background-color: blue;
border: 1px solid white;
padding:10px;
}
document.getElementsByClassName returns a HTMLCollection object, which is live
So when you call
You changed the underlying document and that item is not in the collection anymore (the blockSet[0] is now the second item in your original selection).
what you were doing wrong is every time you change the class the colllection re-evaluates and decreases in size.
Definition and Usage
The getElementsByClassName() method returns a collection of all elements in the document with the specified class name, as a NodeList object.
The NodeList object represents a collection of nodes. The nodes can be accessed by index numbers. The index starts at 0.
And by just adding only
blockSet[0].className = "block-selected";
and by clicking on the button it colord each div by each click so we need to click 8 times in order to color all the divs and see the live example belowAnd by adding only
var blockSet = document.getElementsByClassName('block-default'); alert("Length are: " + blockSet.length + "\nFirst Item is: " + blockSet[0].childNodes[0].nodeValue);
without the rest it will alertAs in the below live example:
I hope my post it helps, let me know if you have any question.