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.

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 *