SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Zealot
    Join Date
    Apr 2003
    Location
    Denmark
    Posts
    127
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Different classes on cells inside table

    I have made a function which should make every 1st and 3rd table cell have their own class.
    The table only consists of 3 cell in every row.

    My function dosnīt work a 100%
    Does anybody have any suggetions?
    Itīs like the 1st column dosnt get its class except for the 1st column in the first row.

    Code:
    function employee() {
    
    var table = document.getElementById('employees'); 
    var cells = table.getElementsByTagName('td'); 
    var count = cells.length;
    var c = 0;
    
    for (var i = 0; i < count; i++) { 
    
    c++;
    
    if(c == 1) { cells[i].className = 'left-col';  } // 1 st column
    if(c == 2) { cells[i].width = '9px;' } // 2 nd column
    if(c == 3) { cells[i].className = 'right-col'; }  // 3 rd column
    if(c == 4) {  c = 1; } 
    
    }
    }
    
    onload = employee;

  2. #2
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,789
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    if(c == 4) { c = 0; }

    Otherwise it will only ever be 1 the first time through since you always add one before testing its value.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  3. #3
    SitePoint Zealot
    Join Date
    Apr 2003
    Location
    Denmark
    Posts
    127
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you for your answer (-:

  4. #4
    SitePoint Zealot
    Join Date
    Apr 2003
    Location
    Denmark
    Posts
    127
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Actually the solution for me was.

    Code:
    function employee() {
    
    var table = document.getElementById('employees'); 
    var cells = table.getElementsByTagName('td'); 
    var count = cells.length;
    var c = 0;
    
    for (var i = 0; i < count; i++) { 
    
    c++;
    
    if(c == 4) {  c = 1; } 
    if(c == 1) { cells[i].className = 'left-col';  } // 1 st column
    if(c == 2) { cells[i].width = '9px;' } // 2 nd column
    if(c == 3) { cells[i].className = 'right-col'; }  // 3 rd column
    
    }
    }
    
    onload = employee;
    But you guided me in the right direction felgall
    Thanks again


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
  •