Check the following code
Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Untitled Document</title>
<style type="text/css">
.blue{
width:100px;
height:100px;
border:1px solid blue;
margin-bottom:5px;
color:blue;
font-family:Verdana;
font-size:11px;
}
.red{
width:100px;
height:100px;
border:1px solid red;
margin-bottom:5px;
color:red;
font-family:Verdana;
font-size:11px;
}
.one{
width:100px;
height:50px;
border:1px solid green;
margin-bottom:5px;
color:gray;
font-family:Verdana;
font-size:11px;
}
</style>
<script type="text/javascript">
function getElementsByClass(searchClass, node, tag){
var classElements = new Array();
if (node == null)
node = document;
if (tag == null)
tag = '*';
var els = node.getElementsByTagName(tag);
var elsLen = els.length;
var pattern = new RegExp("(^|\\s)" + searchClass + "(\\s|$)");
for (i = 0, j = 0; i < elsLen; i++) {
if (pattern.test(els[i].className)) {
classElements[j] = els[i];
j++;
}
}
return classElements;
}
window.onload = function(){
var els = getElementsByClass('blue',null,null);
for(var i = 0; i < els.length; i++){
els[i].className = 'red' ;
}
}
</script>
</head>
<body>
<div class="one">Class used is one</div>
<div class="one">Class used is one</div>
<div class="blue">Class used is blue and will be changed to red using JavaScript</div>
<div class="one">Class used is one</div>
<div class="blue">Class used is blue and will be changed to red using JavaScript</div>
<div class="blue">Class used is blue and will be changed to red using JavaScript</div>
<div class="blue">Class used is blue and will be changed to red using JavaScript</div>
</body>
</html>
Is this what you are looking for?
The getElementsByClassName function was developed by Dustin Diaz.
Let me know if this is what you are looking for.
Bookmarks