Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get class name through JavaScript

Tags:

javascript

How do I get the class name through JavaScript given no id with this span.

Like: <span class="xyz"></span>

I want to change the background color of this span.

How can I solve this? Please help me.

tympanus.net/Tutorials/CSS3PageTransitions/index3.html#contact

like image 759
Pritesh Mahajan Avatar asked Oct 17 '11 15:10

Pritesh Mahajan


1 Answers

Something like this should work:

var spans = document.getElementsByTagName("span");
for (var i = 0; i < spans.length; i++) {
    if (spans[i].className == 'xyz') {
        spans[i].style.backgroundColor = '#000';
    }
}
like image 57
endyourif Avatar answered Sep 18 '22 14:09

endyourif