beyerss beyerss - 4 months ago 89
Javascript Question

Javascript tables

I'm having an issue with looping through a table in Javascript and getting the text from the first cell of a row. I want to get the text of this cell so that I can compare it with something else and remove the row if the text matches. When I try to get the text though, all that actually comes out is "[object text]". Can anyone tell me how to actually get the text. My code below works for looping through all the rows of the table and I think it is getting the correct cell.

function addRow(id, sender, message){
var theTable = document.getElementById('messageTable');
var lastRow = theTable.rows.length;
var newRow = theTable.insertRow(lastRow); = id;

var cellLeft = newRow.insertCell(0);
var textNode = document.createTextNode(id);

var secondCell = newRow.insertCell(1);
var textNode2 = document.createTextNode(sender);

var messageCell = newRow.insertCell(2);
var messageNode = document.createTextNode(message);

var deleteCell = newRow.insertCell(3);
var deleteNode = document.createElement('button');
deleteNode.value = 'Delete';
deleteNode.innerHTML = 'Delete';
deleteNode.onclick = function(){
var myTable = document.getElementById('messageTable');
var rows = myTable.childNodes;

var i=1;
for (i=1;i<rows.length;i++) {
var myCell = rows[i].getElementsByTagName("td")[0];
var myCellText = myCell.childNodes[0];
if (myCellText == id){


This should do it:

var myCellText = myCell.childNodes[0].innerHTML;