Ryan Castle Ryan Castle - 27 days ago 12
HTML Question

Getting innerHTML from an [object, HTMLUnknownElement] with JavaScript (No JQuery)

So, I'm writing a script which finds elements with a tagName of

ui
, and prints to the console the content inside of the tags:

<ui>CONTENT_HERE</ui>


I am using the following code to perform this action.

var e = document.getElementsByTagName('ui');

for (var i = 0; i < e.length; i++) {
if (e[i].tagName.toLowerCase() === 'ui') {
console.log(e[i].innerHTML());
}
}


For this code, I am given the error:

e[i].innerHTML is not a function(...)


On printing out
e[i]
on its own, I am given
<ui>CONTENT_HERE</ui>
as the output.

What could I do to take the content from inside of the tags and print it to the screen?

Note - I have tried using
.html()
and
.value()
also.

NO JQuery Please. Thanks.

Answer

Just remove the parenthesis to access the property.

console.log(e[i].innerHTML);
//                       ^^

var e = document.getElementsByTagName('ui');

for (var i = 0; i < e.length; i++) {
    if (e[i].tagName.toLowerCase() === 'ui') {
        console.log(e[i].innerHTML);
    }
}
<ui>CONTENT_HERE</ui>

Comments