Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Checking if object is a DOM-element [duplicate]

Passing DOM elements to WebWorkers gets tricky since all references to the DOM are 'lost'. I need to check objects that are passed before the WebWorker's message is sent.

What is the fastest way to check if an instance of an object is a DOM-element OR/AND part of the DOM tree, OR has 'children' that contain any references to the DOM tree?

piece of the usage:

var a = new SharedWorker("bigdatahandler.js");   
a.postMessage(s);

s //<--should not be a DOM object
like image 887
Caspar Kleijne Avatar asked Jan 22 '11 12:01

Caspar Kleijne


People also ask

How do you check if an object is a DOM element?

To check if an element is connected or attached to the DOM or the document object ( or otherwise called the context ), you can use the isConnected property in the element's object in JavaScript. The isConnected element property returns boolean true if it connected to the DOM ( document object) and false if not.

What is the best way to locate an element in the DOM?

The easiest way to find an HTML element in the DOM, is by using the element id.

How do I find the ID of a DOM element?

The DOM id Property is used to set or return the id of an element i.e value of the Id Attribute. An ID should be different in a document. It is returned by using the document. getElementById() method.

What is HTML element in JavaScript?

HTMLElement : Element. HTMLElement represents an element in the HTML document tree. HTMLElement is the base type for HTMLDivElement, HTMLSpanElement, HTMLImageElement, and many others.


1 Answers

To check whether an object is an Element instance, use instanceof:

s instanceof Element

And to check its owner document, use ownerDocument:

s.ownerDocument == document
like image 177
Gumbo Avatar answered Oct 21 '22 00:10

Gumbo