Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting value from table cell in JavaScript...not jQuery

I can't believe how long this has taken me but I can't seem to figure out how to extract a cell value from an HTML table as I iterate through the table with JavaScript. I am using the following to iterate:

  var refTab=document.getElementById("ddReferences")   var  ttl;   // Loop through all rows and columns of the table and popup alert with the value   // /content of each cell.   for ( var i = 0; row = refTab.rows[i]; i++ ) {      row = refTab.rows[i];      for ( var j = 0; col = row.cells[j]; j++ ) {         alert(col.firstChild.nodeValue);      }   } 

What is the correct call I should be putting in to the alert() call to display the contents of each cell of my HTML table? This should be in JS...can't use jQuery.

like image 659
GregH Avatar asked Jun 18 '10 18:06

GregH


People also ask

How to get particular td value in JavaScript?

var t = document. getElementById("table"), d = t. getElementsByTagName("tr"), r = d. getElementsByTagName("td");

How to get table row td value in JavaScript?

each(function() { valuesByRowID[this.id] = $(this). find("> td"). map(function() { // Option 1: Getting the value of the `value` attribute: return this. getAttribute("value"); // or return $(this).

How to get cell value from table using jQuery?

$('#mytable tr'). each(function() { var customerId = $(this). find("td:first"). html(); });


2 Answers

function GetCellValues() {     var table = document.getElementById('mytable');     for (var r = 0, n = table.rows.length; r < n; r++) {         for (var c = 0, m = table.rows[r].cells.length; c < m; c++) {             alert(table.rows[r].cells[c].innerHTML);         }     } } 
like image 130
brendan Avatar answered Sep 24 '22 17:09

brendan


If I understand your question correctly, you are looking for innerHTML:

alert(col.firstChild.innerHTML); 
like image 37
Sarfraz Avatar answered Sep 24 '22 17:09

Sarfraz