0

I have a report that has some headers on the top of the table(columns) but also some headers on the left of the table (rows). Those headers are variable, never fixed. The whole entire table is built through DOM Scripting based on an Ajax response that returns me the top headers, the left headers, and the qty at a particular intersection.

I already have an algorithm that knows how populate the cells in that table based on the column header and the row header.

In Firefox, Chrome, and Safari the following works:

document.getElementById("myTable").rows[row].cells[column].firstChild.nodeValue = item.qty.toString();

The problem is in IE7 (what the client uses). IE 7 does not let me access a particular cell by using the [index] notation.Basically, its blowing up at ".cells[column]" . Do you guys know the equivalent of the statement above in IE7?

Also do you know of a jQuery way to fill a known cell once I have the row and the column coordinates?

Thanks, -Dario

Chandu
  • 81,493
  • 19
  • 133
  • 134
Viriato
  • 2,981
  • 7
  • 40
  • 54
  • http://stackoverflow.com/questions/376081/how-to-get-a-table-cell-value-using-jquery/376095#376095 – Brandon Boone Jun 24 '11 at 18:16
  • jquery is ok but you can use xpath too http://js-xpath.sourceforge.net/ – aalku Jun 24 '11 at 18:26
  • can you show more of your js/markup? – hunter Jun 24 '11 at 18:34
  • Thanks a lot everybody, I tried your jQuery approaches and they worked. Additionally, what I thought it did not work in IE7. It does work in IE 7 but I have found that IE7 wants you to create the table with a **thead** tag and a **tbody** tag as well. Once I included the thead and tobdy tags. The following also worked in IE7: document.getElementById("myTable").rows[row].cells[column].firstChild.nodeValue = item.qty.toString(); Sorry if I wasted your time but your answers were also a solution to the problem. – Viriato Jun 24 '11 at 19:21

5 Answers5

1
jQuery("#myTable tbody tr:eq(" + row + ") td:eq(" + column + ")").html("foo");
hunter
  • 62,308
  • 19
  • 113
  • 113
epascarello
  • 204,599
  • 20
  • 195
  • 236
0

row and column will start from 0. Use the above codes.

Troy SK
  • 1,279
  • 7
  • 14
0

Non-jquery way:

document.getElementById("myTable").getElementsByTagName('tbody')[0].getElementsByTagName('tr')[row].getElementsByTagName('td')[column].innerHTML = item.qty.toString();
James
  • 20,957
  • 5
  • 26
  • 41
0
$("#myTable tr:eq(" + row + ") td:eq(" + column + ")").html(item.qty.toString());

However you should be aware of nested tables. Especially if you have another table inside the table with the myTable id. If so let me know, i might rewrite the line for you.

Also you should be aware that I was using jQuery here. To add jQuery to your page you should add the following to the page, preferably in the <head> element:

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js"></script>

This if you want to make use of the jQuery file hosted by google, but of course you can download it and host it yourself if you wish so.

khel
  • 2,358
  • 2
  • 19
  • 8
-1

Try this:

$("#myTable tr:eq(" + row + ") td:eq(" + columnh + ")").html(item.qty.toString());
Chandu
  • 81,493
  • 19
  • 133
  • 134