Results 1 to 2 of 2

Thread: class on every other div

  1. #1
    Join Date
    Aug 2009
    Posts
    9
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default class on every other div

    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.

  2. #2
    Join Date
    Dec 2008
    Location
    Portsmouth, UK
    Posts
    1,891
    Thanks
    2
    Thanked 441 Times in 435 Posts

    Default

    Code:
    <!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>
    Vic
    God Loves You and will never love you less.
    http://www.vicsjavascripts.org/Home.htm
    If my post has been useful please donate to http://www.operationsmile.org.uk/

  3. The Following User Says Thank You to vwphillips For This Useful Post:

    zbphill (09-22-2009)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •