Reputation: 894
I wish to check if an argument passed to my function is an HTMLElement, using the instanceof operator. I know that this doesn't work in IE7, because AFAIK IE7 doesn't define HTMLElement. I figured: no problem, I'll just fall back to a parent prototype like Node. But, as answers on StackOverflow have learned me, the Node-object is also not defined in IE7
What now is the best way to test if my parameter is a node/HTMLElement? Am I doing it wrong?
Upvotes: 1
Views: 867
Reputation:
I have not used IE7 in ages, but I would check with a if (typeof elementNode == 'element')
It might be 'element' or 'HTMLElement'. To make sure first do an alert(typeof elementNode);
to know what its type will be. So then you can compare against it.
Edit: first commenter clarified ↑ this should not work.
Then I would try checking if there is such a member like nodeType or nodeName.
Maybe with something like if (elementNode.nodeName !== null)
just as example. Always try alert()ing before.
Upvotes: 0
Reputation: 17899
In the Node interface HTMLElement
corresponds to nodes of type 1.
const unsigned short ELEMENT_NODE = 1;
http://www.w3.org/TR/DOM-Level-2-Core/core.html#ID-1841493061
Thus, you should check for the argument's nodeType
:
if (arg && 1 === arg.nodeType) { /* ... */ }
Upvotes: 2