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.