tags:

views:

42

answers:

1

I would like to pull data (string) from a column called "Limit" in a table ("displayTable") in javascript. How do I do that?

var table = document.getElementById('displayTable');    
var rowCount = table.rows.length;    
for (var i = 1; i < rowCount - 1; i++) {    
     var row = table.rows[i]["Limit"].ToString();
     alert(row);
     ...
}
+1  A: 

This is how I accomplished reading a table in javascript. Basically I drilled down into the rows and then I was able to drill down into the individual cells for each row. This should give you an idea

var oTable = document.getElementById('myTable');
//gets table

var rowLength = oTable.rows.length;
//gets rows of table

for (i = 0; i < rowLength; i++){
//loops through rows

   var oCells = oTable.rows.item(i).cells;
   //gets cells of current row
   var cellLength = oCells.length;
       for(var j = 0; j < cellLength; j++){
       //loops through each cell in current row
          <!--get your cell info here--> 
          <!--var cellVal = oCells.item(j).innerHTML;-->      
       }
}

UPDATED - TESTED SCRIPT

    <table id="myTable">
        <tr>
            <td>A1</td>
            <td>A2</td>
            <td>A3</td>
        </tr>
        <tr>
            <td>B1</td>
            <td>B2</td>
            <td>B3</td>
        </tr>
    </table>
<script>
    var oTable = document.getElementById('myTable');
    //gets table

    var rowLength = oTable.rows.length;
    //gets rows of table

    for (i = 0; i < rowLength; i++){
    //loops through rows

       var oCells = oTable.rows.item(i).cells;
       //gets cells of current row
       var cellLength = oCells.length;
           for(var j = 0; j < cellLength; j++){
           //loops through each cell in current row
              <!--get your cell info here-->
              var cellVal = oCells.item(j).innerHTML;
              alert(cellVal);
           }
    }
</script>
Jeff V
I have made progress with the snippet. innerHTML seem to be empty which is strange because that is how I added the data when entering the data in my javascript code. var cell4 = row.insertCell(3); cell4.innerHTML = limit;
cell4.innerHTML = limit;What is limit?I think if you assign a variable to cell4.innerHTML you will get what is in that cell (assuming you set everything up correctly and there is actual data).
Jeff V
OK I just created a test page and tested the script. I edited my original answer to alert our each cell in the table. From here I think you should be able to get what you are needing.
Jeff V
I see where I was going wrong. The for loop didnt need a rowLength - 2... in my case at least. I really appreciate this
@user54197: the -2 on the rowLength in my original snippet was remnant of the code I had used prior... I'm glad it worked for you!
Jeff V