When i tried to create an instance of DOM's HTMLElement,
var oElement = new HTMLElement();
It throws : TypeError: Illegal constructor
Why can't we instantitate DOM's Constructor Functions ? Is there a way to do it? Thanks
To create a new element with Javascript you use the createElement method of the document object.
var newDiv = document.createElement("div");
To add some new text with Javascript you can use the createTextNode method of the document object.
var text = document.createTextNode("Text goes here");
Object.create(HTMLElement.prototype, {})
Most DOM constructors are not supposed to be constructors, but just holders for their prototype interface. They will however have factory functions to construct them, which also validate the arguments.
In your particular case, an HTMLElement
does a) need a tagname and b) a document to associate the node with. The createElement
method of document
takes both.
var element=document.createElement('yourHTMLElement');