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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Access Dynamic Table Names 1

Status
Not open for further replies.

Mannga

Programmer
Jun 21, 2002
85
GB
Hi All,

I have a ASP page that is creating a list of tables which have the following naming convention

tblCaseDetails? where the ? is a number

I need to be able to access the data that is stored in the first cell of each table using javascript.

I have the following

alert(document.all.tblCaseDetails0.rows[0].cells[0].innerText);

Which will give me the info in the first cell of the table named "tblCaseDetails0".

How can I change this so that no matter how many tables are created I can always access them from Javascript?

I though of something like

document.all.tblCaseDetails + i +.rows[0].cells[0].innerText;

Where 'i' is the number but this does not work.

This is the last piece of a project that I would love to get away from so all help will be greatly appreciated.

Thanks,
Gavin
 
<script>
function getInfo(){
allTables = document.getElementsByTagName(&quot;table&quot;)
for (x=0; x<allTables.length; x++){
if (allTables[x].id.substr(0,14) == &quot;tblCaseDetails&quot;){
alert(allTables[x].firstChild.firstChild.firstChild.innerHTML)
}
}
}
</script>
<body onLoad=&quot;getInfo()&quot;>

Programming today is a race between software engineers striving to build better and bigger idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. - Rich Cook
 
firstChild is the answer I was looking for before, I can skip the mess I was making now... thanks..

PS: Is there anything like a secondChild?...

I have this
<table id=&quot;tblTest&quot;>
<tr>
<td>
<table id=&quot;tblDetails1&quot;>
<tr>
<td id=&quot;tdID&quot;>
4
</td>
</tr>
<tr>
<td id=&quot;tdName&quot;>
Gavin
</td>
</tr>
<etc.......>
</table>
</td>
</tr>
</table>

I can now access the '4' using firstChild but how do I access the 'Gavin'?
 
There is a next child

consider the table

<table id=&quot;myTable&quot;>
<tr>
<td>cell 1</td>
<td>cell 2</td>
</tr>
<tr>
<td>cell 3</td>
<td>cell 4</td>
</tr>

<script>
//cell 4 can be referenced in several ways
tableNode = document.getElementById(&quot;myTable&quot;)

//method 1
//remember that the first child of a tableNode is always
//the TBODY node even though you don't write it
val1 = tableNode.firstChild.firstChild.nextSibling.firstChild.nextSibling.innerHTML
//literally
//table.tableBody.firstRow.secondRow.firstCell.secondCell.innerHTML

//method 2
//here you use the rows array followed by the cells array
val2 = tableNode.firstChild.childNodes[1].childNodes[1].innerHTML


// you could also cheat using the lastChild nodes
//since we want the last child row and last child cell

val3 = tableNode.lastChild.lastChild.lastChild.innerHTML

</script>

Programming today is a race between software engineers striving to build better and bigger idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. - Rich Cook
 
lastChild :)...

Thanks a mill... I get to go home now...

Cheers,
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top