UserTMP101010 UserTMP101010 - 2 months ago 21
Javascript Question

Print content of element to console

I have a feed that is outputting content dynamically to an element. I want to take the text from element A and output it to the console log.

Example:

<div class="elementa">ID5667</div>


Console Output:

ID : ID5667

I've tried a few things, but I'm either getting undefined or the full HTML of that element.

Answer

I think below should work for you.

var result = document.getElementsByClassName("elementa")[0].innerHTML;

console.log(result);

For more reference : getElementByClassName