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;
}
This error is because you need to do
You can do:
Your error occur because .className return a live HTMLCollection. So when you do something like this:
Your collection
blockSet[0]
will become theblockSet[1]
. So when you execute the line :You not change the
blockSet[1]
you think but you change the startingblockSet[2]
.So you can do this:
http://jsfiddle.net/94dqffa7/
I think it's the best way to do it. Because the
classList.add()
andclassList.remove()
will help you to change your class without eleminate the others class you have on your div (if you have some). Also, you need to add the new one before remove the old one or you will have the same problem as before.The simplest way you can do this is by using the below code:
Instead of using
getElementsByClassName()
,which returns a live HTMLCollection that will change as the
className
s change,you can use
querySelectorAll()
,which returns a non-live NodeList that will not change.
querySelectorAll()
has better IE support thangetElementsByClassName()
(IE8+ vs. IE9+).It's also much more flexible since it supports CSS selectors (CSS2 for IE8+ and CSS3 for IE9+).
However,
querySelectorAll()
is slower thangetElementsByClassName()
.Keep that in mind if you're processing thousands of DOM elements.
Snippet
By assigning a value to
.className
you overwrite every class on that element. What you might want to take a look at is the.classList
attribute.Remove a class:
Add the new class:
A good point to start with, when your trying to do stuff, jQuery usually did for you, is http://youmightnotneedjquery.com/
You have already some good solutions.
I think that the best one is the one from Rick Hitchcock.
But a solution that I often use, to be safe when doing things like that, is to travel the collection backwards
That isolates you from changes in the collection