Re: Convenient way to create element and set its attributes at once

On Sun, Dec 1, 2013 at 8:05 PM, Elliott Sprehn <esprehn@gmail.com> wrote:
> I believe we should just solve this with the constructor syntax:
>
> new HTMLDivElement({
>   id: "foo"
> });
>
> and
>
> new HTMLHeadingElement("h1", {
>   id: "foo"
> });

Agreed.

Note that when this has come up previously, there's been need to set
both attributes and properties.  It's confusing, unfortunately, but
necessary - for example, if you want to set an onclick handler from
the constructor with a function, you have to assign the onclick
property; if you have a string containing code, you have to assign the
onclick attribute.  Some controls you want to be able to set on an
element are only accessible via a property.

Note as well that when CSS sprouted a namespace object on window, one
of the planned purposes was to host short-named constructors for the
various Value API thing we'll need, like "new CSS.px(5)" rather than
"new CSSPixelComponentValue(5)".  It would be useful for HTML to do
the same, so you could call "new HTML.div(...)" instead of "new
HTMLDivElement(...)".  It would also let us establish a one-to-one
constructor to tagname correspondence, so "HTML.h1()" works instead of
"HTMLHeadingElement('h1',...)".

~TJ

Received on Monday, 2 December 2013 18:27:15 UTC