Don't use .eq(1)[0]
, but just .get(0)
to get plain DOM nodes. Also, don't use two different ways of obtaining the same text node, but only one and store it in a variable. Let's check what has happened:
$('.my-table tr').each(function() {
var cell = $('td', this);
if (!cell.length)
return alert("Could not find a table cell");
var el = cell.get(0);
if (!el) alert("Could not get first element"); // Won't happen if length was >0
if (!el.childNodes.length)
return alert("Cell is empty!");
var text = el.childNodes[0];
if (cell.contents()[0] != text) alert("different firstChilds???"); // Won't happen
if (text.nodeType != 3)
return alert("the first child node is not a text node!");
var contact = text.nodeValue;
if (text.data != contact) alert("different contents???"); // Won't happen
if (typeof contact != "string") alert("content is no string"); // Won't happen
var newcontact = contact.substring(0,10);
alert('"'+contact+'" was changed to "'+newcontact+'"');
text.data = newcontact;
});
(Demo at jsfiddle.net)
me@business.com