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

"Tab Atkins Jr." <[email protected]>
Newsgroups gmane.comp.web.dom.general
Message-ID <CAAWBYDDCPFWc5kns_+8FJh7ea0uM86ZxaVB9Q8NYsArgji1a6Q@mail.gmail.com>
On Sun, Dec 1, 2013 at 8:05 PM, Elliott Sprehn <[email protected]> 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
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.