简体   繁体   中英

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.

** 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM