PDA

View Full Version : class on every other div



zbphill
09-11-2009, 03:13 PM
Looking for a script that places a class on every other div.

I have a row of divs and need to place class=left on one div, then class=right on the following and so on.

Any help would be great.

vwphillips
09-11-2009, 03:44 PM
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

<head>
<title></title>
<style type="text/css">
/*<![CDATA[*/
.left {
background-Color:red;
}

.right {
background-Color:green;
}

/*]]>*/
</style>
<script type="text/javascript">
/*<![CDATA[*/
function Cls(id,c1,c2){
var ds=document.getElementById(id).getElementsByTagName('DIV');
for (var z0=0;z0<ds.length;z0++){
ds[z0].className=z0%2==0?c1:c2;
}

}
/*]]>*/
</script>
</head>

<body onload="Cls('tst','left','right');">
<div id="tst" >
<div>1</div>
<div>2</div>
<div>3</div>
<div>4</div>
</div>
</body>

</html>