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 CS
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 than getElementsByClassName()
(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 than getElementsByClassName()
.
Keep that in mind if you're processing thousands of DOM elements.
Snippet
var blockSet = document.querySelectorAll(".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";
.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;
}
<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>
This error is because you need to do
blockSet[0].className = 'block-selected'
You don't to write blockSet[1] , blockSet[2] ...
You can do:
for (var s=0;s<blockSetLength;s++) {
blockSet[0].className = 'block-selected';
}
Use this to your javascript code. this will fix your error
<script>
var blockSet = document.getElementsByClassName("block-default");
var blockSetLength = blockSet.length;
for (var i = blockSet.length - 1; i >= 0; i--) {
blockSet[i].className = "block-selected";
};
</script>
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:
blockSet[0].classList.remove('block-default');
Add the new class:
blockSet[0].classList.add('block-selected');
A good point to start with, when your trying to do stuff, jQuery usually did for you, is http://youmightnotneedjquery.com/
The simplest way you can do this is by using the below code:
while(blockSetLength--){
//this will change the class of n-1 dom object
blockSet[blockSetLength].className='block-selected';
}
This worked for me
var blockSet = document.getElementsByClassName("block-default");
var blockSetLength = blockSet.length;
blockSet[0].className = "block-selected";
blockSet[0].className = "block-selected";
blockSet[0].className = "block-selected";
blockSet[0].className = "block-selected";
blockSet[0].className = "block-selected";
blockSet[0].className = "block-selected";
blockSet[0].className = "block-selected";
blockSet[0].className = "block-selected";
.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;
}
<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>