How to get an element’s tag name in JavaScript?

Sometimes, we want to get an element’s tag name in JavaScript.

In this article, we’ll look at how to get an element’s tag name in JavaScript.

How to get an element’s tag name in JavaScript?

To get an element’s tag name in JavaScript, we can use an element’s nodeName property.

For instance, we write:

<div>

</div>

to add a div.

Then we write:

console.log(document.querySelector('div').nodeName)

to select the div with document.querySelector.

And use the nodeName property to return the node’s tag name as a string.

As a result, we should see 'DIV' logged.

Conclusion

To get an element’s tag name in JavaScript, we can use an element’s nodeName property.