Categories
JavaScript Answers

How to test the type of a DOM element in JavaScript?

Spread the love

Sometimes, we want to test the type of a DOM element in JavaScript.

In this article, we’ll look at how to test the type of a DOM element in JavaScript.

How to test the type of a DOM element in JavaScript?

To test the type of a DOM element in JavaScript, we can check the nodeName property of the element.

For instance, we write

if (element.nodeName === "A") {
  //...
} else if (element.nodeName === "TD") {
  //...
}

to check if element is an a element with

element.nodeName === "A"

And we check if element is a td element with

element.nodeName === "TD"

Conclusion

To test the type of a DOM element in JavaScript, we can check the nodeName property of the element.

Leave a Reply

Your email address will not be published.

If you like the content of this blog, subscribe to my email list to get exclusive articles not available to anyone else.