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

02.12.2013, 22:26, "Tab Atkins Jr." <jackalmage@gmail.com>:
> 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.

Setting properties and adding event listeners _right after_ an element is created are probably not as common as setting its attributes (even aside from the fact that elements and attributes are basic units of HTML while properties and events are sort of another conceptual "layer").

So it's probably not necessary to incorporate them into constructor itself, and it would probably be sufficient just to have dedicated methods for setting multiple properties / adding multiple event listeners at once like:

    element.setProperties(propMap);
    element.addEventListeners(eventHandlersMap);

Received on Tuesday, 3 December 2013 00:12:14 UTC