• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1876
  • Last Modified:

Use JavaScript to get contents of Cells in HTML table

Hi I'm trying to list all the contents of a table using Javascript.as in the code below.

I can get the contents of 1 cell, but am curious ot know why I can;t use a For Loop to go through each row using the code I have...see below

There seems to be a problem with y=x[g].cells; ... it give me the content of the first cell but seems to stop the loop and not continue?

What am i missing?
function updatePrices(){
 
    var oRows = document.getElementById('tblCart').getElementsByTagName('tr');
    var iRows = oRows.length;
    var x=document.getElementById('tblCart').rows;
    var y=null;
    
    for (g=1;g<=iRows;g++)
    { 
       y=x[g].cells;
       document.write(y[1].innerHTML); 
        // writes content of cell(1,1) but why not go to (2,2) etc??
       //there are 8 rows in the table and if I replace the inner for loop with document.write(g)..it 
       //  writes out 1..8
   
    }
 
}

Open in new window

0
thegunner
Asked:
thegunner
  • 2
1 Solution
 
David H.H.LeeCommented:
Hi thegunner,
Perhaps you can post your table design here for further checking. However, i've tested with this attempt and it can display the output as thought. Please provide more details.

Code
--------------------
<script>
function updatePrices(){
 
    var oRows = document.getElementById('tblCart').getElementsByTagName('td');
    var iRows = oRows.length;
    var x=document.getElementById('tblCart').rows;
    var y=null;
   
    for (g=0;g<=iRows;g++)
    {
       y=x[g].cells;
       alert(y[1].innerHTML);
      
       // writes content of cell(1,1) but why not go to (2,2) etc??
       //there are 8 rows in the table and if I replace the inner for loop with document.write(g)..it
       //  writes out 1..8
   
    }
 
}
</script>
<body>
<table id="tblCart" name="tblCart">
<tr>
 <td>ROw 1</td>
 <td>ROw 1.1</td>
</tr>
<tr>
 <td>ROw 2</td>
 <td>ROw 2.2</td>
</tr>
<tr>
 <td>ROw 3</td>
 <td>ROw 3.3</td>
</tr>
<tr>
 <td>ROw 4</td>
 <td>ROw 4.4</td>
</tr>
</table>
<script>
updatePrices();
</script>
</body>
0
 
thegunnerAuthor Commented:
Hi,

After much testing! yours will work id you call the updatePrices sub by

<script>
updatePrices();
</script>

If though you call updatePrices() by onClick via a button i.e. replace the above with
<input type="button" value="Update Cart" onclick="updatePrices()"/>
it stops after 1 document.write? ...any ideas how to solve this?
0
 
thegunnerAuthor Commented:
Actually, don;t need to use the document.write I can just assign as cell's value.

But thanks for letting me know that that worked!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now