W3C home > Mailing lists > Public > public-webapps@w3.org > July to September 2011

Re: Element.create(): a proposal for more convenient element creation

From: Maciej Stachowiak <mjs@apple.com>
Date: Mon, 01 Aug 2011 23:17:47 -0700
Cc: Charles Pritchard <chuck@jumis.com>, Ian Hickson <ian@hixie.ch>, WebApps WG <public-webapps@w3.org>
Message-id: <F1E43EFB-3938-4528-9DF5-FC760FCAE349@apple.com>
To: "Tab Atkins Jr." <jackalmage@gmail.com>

On Aug 1, 2011, at 8:43 PM, Tab Atkins Jr. wrote:

> On Mon, Aug 1, 2011 at 7:05 PM, Charles Pritchard <chuck@jumis.com> wrote:
>> Can we have it 'inherit' a parent namespace, and have chaining properties?
>> 
>> Element.create('div').create('svg').create('g').create('rect', {title: 'An svg rectangle in an HTML div'});
> 
> Ooh, so .create is defined both on Element (defaults to HTML
> namespace, just creates an element) and on Element.prototype (defaults
> to namespace of the element, inserts as a child)?  That's pretty
> interesting.  Presumably the new element gets inserted as a last child
> of the parent.
> 
> I like it.

With just the old propose you could get the same effect like this:

Element.create('div', {}, Element.create('svg', {}, Element.create('g', {}, Element.create('rect', {title: 'An svg rectangle in an HTML div'}))))

Chaining .create() is certainly less verbose. Doesn't work as well for inserting multiple children into an element though.

Regards,
Maciej
Received on Tuesday, 2 August 2011 06:18:15 GMT

This archive was generated by hypermail 2.3.1 : Tuesday, 26 March 2013 18:49:46 GMT