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;
}
First, the below code should do the trick in the simplest way.
Next what goes wrong with your code, or rather what is the interesting thing that is happening:
makes the first block set element no longer a block set element. That leaves you with 7 remaining. Now,
Selects the second one of the remaining ones. Which would be the third one of your complete list. Now you have 6 remaining.
This makes the third one among the remaining, which would be your BLOCK5 into a block selected. And results in you having 5 remaining.
This again finds your fourth one which is BLOCK7 when you count to the fourth among your remaining. And now you have 4 remaining.
blockSet[4] onwards find no such element and fail to execute. This is what happens with your code. Quite interesting. :).
Here is a jsfiddle alerting you the values as they run: https://jsfiddle.net/xz7h57jv/
Because you change the
.className
of theblockSet
which is anHTMLCollection
. The collection that have elements with same class (block-default
) will change when the elements suffers some updates.In other words when you change the
.className
of an element the collection will exclude that element. This means that the size of theHTMLCollection
will decrease . Also the size will increase if an element with that class has beed added to the DOM.To solve this you can always change only the first element
.className
.Notes: Intead of changing class element by element, you can iterate through elements with
for
and change.className
.If you add a new item in DOM (not collection) the size will increase as presented in the example below.
This error occurs because
blockSet
is anHTMLCollection
, which is "live."HTMLCollections
update as the elements on the page update.Every time you swap a
className
, you're makingblockSet
shorter one by one.To solve this problem, just do this instead:
That way you chunk your
HTMLCollection
off one by one.Iteration 1:
[ div1, div2, div3, div4, div5, div6, div7, div8 ]
Iteration 2:
[ div2, div3, div4, div5, div6, div7, div8 ]
Iteration 3:
[ div3, div4, div5, div6, div7, div8 ]
Iteration 4:
[ div4, div5, div6, div7, div8 ]
Iteration 5:
[ div5, div6, div7, div8 ]
Iteration 6:
[ div6, div7, div8 ]
Iteration 7:
[ div7, div8 ]
Iteration 8:
[ div8 ]
Hope that helps!
Use this to your javascript code. this will fix your error
This worked for me
You can find the working code over here
Demo link http://jsfiddle.net/patelmit69/9koxfaLq/1/