W3C home > Mailing lists > Public > www-dom@w3.org > July to September 2011

Re: [DOM4]: Element.create

From: Dominic Cooney <dominicc@chromium.org>
Date: Wed, 21 Sep 2011 10:21:30 +0900
Message-ID: <CAHnmYQ_1GdiQG4zC-5LAO3vh=DDdXzAThg3w8yx5=7M6SgezFQ@mail.gmail.com>
To: www-dom@w3.org
Cc: Charles Pritchard <chuck@jumis.com>, Sean Hogan <shogun70@westnet.com.au>
It would be nice, in addition to HTMLElement.create, if elements could
be created directly using new and the constructor taking similar
arguments, eg

HTMLElement.create('div', {...}, ...)

is the same as

new HTMLDivElement({...}, ...)

The reason this is nice is that if you mistype the element name, you
get an error message much closer to the typo. It would be nicer still
if the constructors names were short, eg Div instead of
HTMLDivElement.

Needless to say, HTMLElement.create is useful like
document.createElement is useful, for creating unknown elements or for
reflecting on tag names (although the .constructor property could be
used for that instead.)

> Can you give some proper examples where Element.create() makes DOM
> generation simpler? I've only seen vague hand-waving up to now.

To me I think this:

document.body.appendChild(new Div({className: 'warning'}, [new
Text('Danger, Will Robinson!']));

Is preferable to this:

var d = document.createElement('div');
d.className = 'warning';
d.textContent = 'Danger, Will Robinson!';
document.body.appendChild(d);

The former is mercifully only four lines because of textContent; if it
contained markup, it would be even more verbose.

Dominic
Received on Wednesday, 21 September 2011 01:21:57 GMT

This archive was generated by hypermail 2.2.0+W3C-0.50 : Friday, 22 June 2012 06:14:08 GMT