How to instantiate DOM Constructor functions?

When I tried to instantiate the DOM HTMLElement,

var oElement = new HTMLElement(); 

It throws: TypeError: Illegal constructor

Why can't we perform the functions of the DOM constructor? Is there any way to do this? Thanks

+7
javascript dom html
source share
3 answers

To create a new element with Javascript, you use the createElement method of the document object.

 var newDiv = document.createElement("div"); 

To add new text using Javascript, you can use the createTextNode method of the document object.

 var text = document.createTextNode("Text goes here"); 
+6
source share
 Object.create(HTMLElement.prototype, {}) 
+6
source share

Most DOM constructors do not have to be constructors, but just holders for their prototype interface. However, they have factory functions for building them, which also check the arguments.

In your particular case, the HTMLElement does: a) a tag is required and b) a document to link node to. The createElement document method accepts both.

+5
source share

All Articles