Handle HTMLCollection elements

0

I wonder if with just javascript I can handle the elements of an HTMLCollection in the same way I would with using the jQuery selector.

For example, get the result of document.getElementsByClassName('ativo') and add all the found elements to the ' greater ' class.

    
asked by anonymous 18.01.2018 / 14:46

1 answer

1

Of course, you just have to iterate over the result and add the desired class. Here's a simple example in which iterate the result with for :

const itens = document.getElementsByClassName('ativo');

for (let item of itens) {
  item.classList.add("maior");
}
.maior {
  color: red;
}
<div class="ativo">Texto 1</div>
<div class="ativo">Texto 2</div>
<div class="ativo">Texto 3</div>
<div class="ativo">Texto 4</div>
    
18.01.2018 / 15:00