Categories
JavaScript Answers

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

Spread the love

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.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *