S
S
spmbt2011-10-28 22:14:31
JavaScript
spmbt, 2011-10-28 22:14:31

How to distinguish between DOM object and Object object in JS?

How to confidently and cross-browser distinguish between these objects?
You can use properties: .id==string, .attributes==array, but this is indirect.
instanceof something - most likely, there is no cross-browser class.
Who knows the recommendations from the standardizers? Who uses what methods?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
O
oraz_ra, 2011-10-28
@oraz_ra

var isDomElement = document.getElementById('someId') instanceof HTMLElement;
the most important thing is that it is impossible to create an object of the HTMLElement class and its descendants just like that; you can't write
var el = new HTMLElement();

S
spmbt, 2017-07-11
@spmbt

> document.body+''
"[object HTMLBodyElement]"
> document.querySelector('div')+''
"[object HTMLDivElement]"
> document.querySelector('span')+''
"[object HTMLSpanElement]"

Or the same thing - .toString() (this is in Firefox) - is starting to become some kind of standard. In the past, JQuery 1.x defined a DOM element by having an .attributes property when browsers had a wide range of results.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question