0

Looking to see how you go about checking to see if a parameter that is passed into a function is a HTML element like <div> or <p>, etc... I've tried numerous things but nothing is working:

if (param !== HTMLElement) return undefined;
if (param !== Element) return undefined;
if (param !== Document) return undefined;

There has been some others but nothing seems to work - any pointers in the right direction would be great. thanks

Barmar
  • 741,623
  • 53
  • 500
  • 612
s73c
  • 3
  • 4
  • have you tried making an array of html elements you want to check then comparing the results in something like an if statement? – DigitalDesigner Nov 18 '22 at 01:29
  • `param !== HTMLElement` means: _“Is `param` not the `HTMLElement` constructor?”_. It doesn’t check if `param` is an _instance of_ that constructor. [`instanceof`](//developer.mozilla.org/en/docs/Web/JavaScript/Reference/Operators/instanceof) does. – Sebastian Simon Nov 18 '22 at 01:40

1 Answers1

0

Use the instanceof operator.

if (param instanceof HTMLElement) {
    return undefined;
}
Barmar
  • 741,623
  • 53
  • 500
  • 612