Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get all elements with the same class

I know that this:

document.getElementsByClassName('class-1')[0]. 

selects the first <div> that has the specify class.

I guess using a for() will get through the whole array of <div>.

Can somebody explain how to create that array ?

I will prefer plain Js.

like image 427
Tbi45 Avatar asked Mar 11 '13 14:03

Tbi45


1 Answers

Method getElementsByClassName() returns a set of DOM elements that have a certain class name. Here is a canonical example of how to use the returned list of nodes:

var elements = document.getElementsByClassName("class-1");
for (var i = 0, len = elements.length; i < len; i++) {
    // elements[i].style ...
}
like image 114
VisioN Avatar answered Oct 09 '22 07:10

VisioN