Mntfr Mntfr - 29 days ago 13
HTML Question

Stringify in javascript is returning an empty value

So I have the following code

var email = row.cells[2];
console.log(email);


and that returns
<td>jh@hotmail.com</td>
as an object I think however I need that value as a string in order to remove the
<td></td>
I some websites says that I can use JSON.stringify but when i do it like that it returns
{}
anybody knows why?.

Answer

The value that is returned is not just a normal JavaScript object, it's a special one. It is a DOM node, or more specifically a DOM element. It is the way the page is represented in JS.

Because of this, JSON.stringify() returns {}. Luckily, DOM elements have their own way of getting contents. To get the text content of a DOM element, use .textContent. In this case, it would be

var DOM_Element = row.cells[2];
var email = DOM_Element.textContent
console.log(email);

Consider also innerHTML

var email = row.cells[2].innerHTML
console.log(email);

Credit to @vlaz's comment.