Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

getElementById...innerHTML

Status
Not open for further replies.

hunt00

Technical User
Mar 6, 2005
79
US
If i call the tableID in the following js code in a loop since the total table number is dynamic, it gives error: document.getElementById(tableID).rows has no properties.
In the html code, there is matching table1, table2, table3.... if i hardcode it, it works. But i need to put it in a loop, can anyone give me a help? Thanks!


count = 3; //set 3 for example
for (i = 1; i<=count; i++) {
tableID = 'table' + i;
x = document.getElementById(tableID).rows[0].cells[0].innerHTML;

//hardcode the id works, but i really need a loop
//x = document.getElementById("table1").rows[0].cells[0].innerHTML;

alert(x);
}


 
hmmmm, not working....but thanks for your idea.
 
it comes out "table1" , gives out the error for the next as mentioned above
 
Is your example above copied and pasted from your code, or did you type it in? ALWAYS copy and paste your code so people here see what the original is.

Lee
 
for (i = 1; i<=3; i++) {
tableID = 'table' + i;
//alert(tableID);
if (document.getElementById(tableID).checked == true) {
//alert(tableID); //went through here
x = document.getElementById(tableID).rows[0].cells[0].innerHTML;
//alert('done'); //cant get here
.....
}

 
Hit that button too soon.

Anyway, if you have a checkbox names table1 and a table named table1, you're asking for undefined behavior. The error message you posted in your original question makes complete sense now with the actual code.

Show your HTML for table1 (checkbox, table, or whatever else you have named that) so we can figure out what a good fix would be.

Lee
 
Are you trying to do something like my example below?
If a checkbox is checked then grab the innerHTML of the first cell in a corresponding table name?
If so, your code works once you alter the name of the checkbox to test if checked.
Code:
<html>
<head>
<title>Landscape Printing</title>
<script type="text/javascript">
function test(){
  for (i = 1; i<=3; i++) {
    tableID = 'table' + i;
    if (document.getElementById('check'+i).checked == true) {
      x = document.getElementById(tableID).rows[0].cells[0].innerHTML;
    }
  }
}
</script>
</head>
<body>

<table id="table1" border=1>
  <tr>
    <td>This is table 1</td><td>C<input type="checkbox" id="check1"></td>
  </tr>
</table>
<table id="table2" border=1>
  <tr>
    <td>This is table 2</td><td><input type="checkbox" id="check2"></td>
  </tr>
</table>
<table id="table3" border=1>
  <tr>
    <td>This is table 3</td><td><input type="checkbox" id="check3"></td>
  </tr>
</table>
<input type="button" value="GO" onclick="test()">
</body>
</html>


It's hard to think outside the box when I'm trapped in a cubicle.
 
Thanks very much!! It works! I mixed the tableID and checkbox ID.
 
Dan your a copy kaht. ;)


It's hard to think outside the box when I'm trapped in a cubicle.
 
Hey, if it works.... It avoids the taboo of mentioning the pentagram shaped symbols.

-kaht

[small]How spicy would you like your chang sauce? Oh man... I have no idea what's goin' on right now...[/small]
[banghead]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top