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

thegunnerAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Software

From novice to tech pro — start learning today.