Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Feb 2007
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Creating array from table column

    Hey guys

    I'm writing an app for my school's site that will make an excel-like spreadsheet in which someone can type how many credits they have in science, etc...

    My problem is that I'm not sure how to store the numeric values of a table column in an array to add them up (in order to calculate the total science credits, for example). I really don't want to make individual IDs for 160 table cells and add them individually

    Could someone point me in the right direction on how to get all of the TDs in a table row in an array?

    Thanks,
    Carlton

  • #2
    Senior Coder shyam's Avatar
    Join Date
    Jul 2005
    Posts
    1,563
    Thanks
    2
    Thanked 163 Times in 160 Posts
    Code:
    tds = tr.getElemensByTagName('td');
    the only problem will be this will fetch all the tds even td's of the tables nested in this tr so its a good practise to filter out those

    Code:
    function getTDs(tr) {
      var tds = tr.getElementsByTagName('td');
      target = [];
      for ( i = 0; i < tds.length; i++ ) {
        if ( tds[i].parentNode == tr ) {
          target.push(tds[i]);
        }
      }
      return target;
    }
    but, its generally an overkill

  • #3
    Senior Coder Arbitrator's Avatar
    Join Date
    Mar 2006
    Location
    Splendora, Texas, United States of America
    Posts
    3,274
    Thanks
    28
    Thanked 271 Times in 265 Posts
    getElementsByTagName() returns an array of matching elements. Example:
    Code:
    var sum = 0, tableCells = document.getElementsByTagName("td");
    
    for (var i = 0; i < tableCells.length; i++) {
      sum += parseFloat(document.getElementsByTagName("td")[i].firstChild.data);
      }
    This script iterates through every td element in the document and adds their content together. firstChild.data is the text node child of the cell; you need to convert it to a number before you can use it.

    Note: shyam beat me to the post.
    For every complex problem, there is an answer that is clear, simple, and wrong.

  • #4
    New Coder
    Join Date
    Nov 2003
    Location
    San Francisco
    Posts
    45
    Thanks
    2
    Thanked 2 Times in 2 Posts
    Give the table containing the credits an ID like "credits"

    Code:
    var totals = new Array()
    
    function credCount() {
    var rows=document.getElementById("credits").getElementsByTagName("tr")
    for(i=0;i<rows.length;i++){
    var cols=rows[i].getElementsByTagName("td")
    for(j=0;j<cols.length;j++){
    if(isNaN(totals[j])){
    totals[j]=0
    }
    totals[j]=parseInt(cols[j].innerHTML,10)+totals[j]
    }
    }
    // Do something with the totals
    }
    Total as many columns as you like.

    CC


  •  

    Posting Permissions

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