41

I have a function:

function Check(o)
{
    alert(/* o is a DOM element ? "true" : "false" */);
}

How can I check if the parameter o is a DOM object or not?

Jonas
  • 109,920
  • 93
  • 295
  • 369
BrunoLM
  • 94,090
  • 80
  • 289
  • 441

6 Answers6

85

A DOM element implements the Element interface. So you can use:

function Check(o) {
    alert(o instanceof Element);
}
Edwin Pratt
  • 745
  • 9
  • 19
David Hellsing
  • 102,045
  • 43
  • 170
  • 208
  • It's too bad it can't replace 'typeof' but it's because they're comparing different things (primitives vs object/constructor) in different ways. 'typeof' returns a unary operator vs 'instanceof' returns a binary (boolean) operator. – HaulinOats Nov 15 '18 at 19:17
  • 3
    I did put upvote this answer in the past but something to note is this won't pass if validated by an Element class from a different context. https://stackoverflow.com/questions/52222237/instanceof-fails-in-iframe So if this check don't work for you can try softer method like @Martin Jespersen – Mark Odey Feb 20 '19 at 21:20
28

Check if the nodeName property exists.

Basically check if it is a Node: look at the DOM lvl 1 specs, check the Node definition.

If you meant it literally when you said Element check for tagName property, look at the Element definition in the same spec

So to recap, do either

function Check(o)
{
    alert(o.tagName ? "true" : "false");
}

to check if it is a DOM Element or

function Check(o)
{
    alert(o.nodeName ? "true" : "false" );
}

to check if it is a DOM Node

Martin Jespersen
  • 24,934
  • 6
  • 54
  • 66
9

Instead of just checking for the existence of a property, I'd check its specific value.

This assumes you're looking for a "type 1" element.

nodeType at MDC(docs)

function Check(o) {
    alert( o && o.nodeType && o.nodeType === 1 );
}

You could still get an object that has the nodeType property that isn't actually a DOM node, but it would also have to have a matching value of 1 to give a false positive.

user113716
  • 310,407
  • 61
  • 442
  • 435
4

Late answer, but a document fragment could be a node as well:

function isNode(node) {
    return node && (node.nodeType === 1 || node.nodeType == 11);
}

Credits: https://github.com/k-gun/so/blob/4.8.1/so.dom.js#L50

K-Gun
  • 10,689
  • 2
  • 52
  • 56
0

You can check if a DOM node is element with JQuery:

element.is("*")
zhy2002
  • 410
  • 5
  • 11
0

You can use the following function

function isNode(o)
{
  return o && 'nodeType' in o;
}
Ben Rowe
  • 27,548
  • 6
  • 52
  • 73
  • 1
    I believe it should be `'nodeType' in o`, but nice clean idea! – Nate Barr Mar 18 '13 at 01:28
  • @BenRowe: Nate Barr is right, `'nodeType'` should be in quotes, if not, your code leads to _"ReferenceError: nodeType is not defined"_, so please edit your post! – Sk8erPeter May 09 '13 at 17:41
  • Thanks, I'll make the adjustment – Ben Rowe May 10 '13 at 10:51
  • This will not check that the object is an element, it checks if it's a node. See Constantes section here https://developer.mozilla.org/en-US/docs/Web/API/Node/nodeType – Sangimed Mar 24 '18 at 02:40