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

Marat Tanalin <[email protected]>
Newsgroups gmane.comp.web.dom.general
Message-ID <[email protected]>

02.12.2013, 22:26, "Tab Atkins Jr." <[email protected]>:
> 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.

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);
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.