-2

as much as the question seems silly (and in fact it is) I didn't find the answer anywhere, only what I find is answers to know if the element already exists in the DOM (which would solve my problem, but that would be a way unprofessional to solve).

Here is my code ex :

function randomName(element) {
// Im trying to find some method like this:
  const result = isElement(element) ? true : false
}
sigleane
  • 63
  • 6
  • 2
    Duplicate of [How can I check if an element exists in the visible DOM?](https://stackoverflow.com/questions/5629684/how-can-i-check-if-an-element-exists-in-the-visible-dom) – esqew Jan 19 '22 at 15:47
  • the question isn't duplicated. I made it clear that methods like this don't answer the question, they just solve it – sigleane Jan 19 '22 at 15:50

1 Answers1

2

You might want to check if the variable is an instance of HTMLElement. So basically

function isElement(element){
    return element instanceof HTMLElement;
}

Also check the mdn-docs, because it's possible that you want to check if your element is a Node (would include text-only nodes) or just an Element (that would include XML-tags in an XML document).

boppy
  • 1,753
  • 12
  • 10