Gynteniuxas
Gynteniuxas

Reputation: 7103

Javascript: get content of table's cell (inside div)

I have a little problem with finding cell's id (and its content). Main table contains multiple divisions and each division contains one table with several cells. What I want is to display cell with id content_xx. My tables look like this:

<table id="MainTable">                  // Main (global) table

 <div id="divid_1">                     // First div in main table
  <table id="InfoTable">                // First div's table
   <td id="title_10">CellTitle</td>     // Cell #1
   <td id="content_11">CellContent</td> // Cell #2

 <div id="divid_2">                     // Second div in main table
  <table id="InfoTable">                // Second div's table
   <td id="title_20">CellTitle</td>     // Cell #1
   <td id="content_21">CellContent</td> // Cell #2

 // etc...
</table>

Function for finding it:

function FindContent(id)
{
    t = document.getElementById("InfoTable").rows[0].cells.namedItem("content_"+id).innerHTML;
    alert(t);
    return;
}

I have onclick event which executes this function. It works for me only for first InfoTable, but it does not work when I try same thing on table #2 or further. I get this error: TypeError: document.getElementById(...).rows[0].cells.namedItem(...) is null. How can I fix this and why it does not work? Is it because of row[0]? If I change to row[1], for example, then it doesn't work at all. I can change structure of html but not completely.

Upvotes: 1

Views: 972

Answers (1)

Gynteniuxas
Gynteniuxas

Reputation: 7103

** Solved by icecub **

The problem was that I was using same ID in divs, therefore, only first table would have been returned. I just needed to use different ID.

Upvotes: 1

Related Questions