PDA

View Full Version : getElementById with for loop



mrconestoga
04-01-2010, 05:19 PM
In an effort to string together cell info from multiple rows in a table, I'm wondering if it possible to use getElementById to accomplish this? My desired outcome of test code included is:
AAA1
BBB2

I am attempting to work my way to AAA1AAA2BBB1BBB2 string that will be passed in a hidden field to a third party package. The sample code works when I limit to one row but errors when I add the for loop.




<html>
<head>
<script type="text/javascript">
function showRows()
{
for (iii=0;iii<=1;iii++)
{
var x=document.getElementById('myTable').rows[iii].cells;

document.write(x[0].innerHTML);
document.write("<br />");
}
}
</script>
</head>
<body>

<table id="myTable" border="1">
<tr>
<td>AAA1</td>
<td>AAA2</td>
</tr>
<tr>
<td>BBB1</td>
<td>BBB2</td>
</tr>
</table>
<br />
<input type="button" onclick="showRows()" value="Show rows in table">

</body>
</html>

Nile
04-01-2010, 05:38 PM
Here:


<script type="text/javascript">
var add = function(table, out){
for(var i = 0; i < table.rows.length; i++){
for(var a = 0; a < table.rows[i].cells.length; a++){
out.value += table.rows[i].cells[a].innerHTML;
}
}
};
</script>
<input type="text" id="text" value="" />
<table id="table">
<tr><td>AAA1</td><td>AAA2</td></tr>
<tr><td>BBB1</td><td>BBB2</td></tr>
</table>
<input type="button" onclick="add(document.getElementById('table'),document.getElementById('text'));" value="Add table up" />

mrconestoga
04-01-2010, 05:56 PM
Nile,

That is a great example. Thank you for your help!

Gary

Nile
04-01-2010, 06:07 PM
No problem, glad to help.

It seems your topic is solved... Please set the status to resolved.. To do this:
Go to your first post ->
Edit your first post ->
Click "Go Advanced" ->
Then in the drop down next to the title, select "RESOLVED"