misterzorgy
misterzorgy

Reputation: 123

How to dynamically create Polymer custom element after setting all attributes?

The problem is when I try to create custom element this way

var el= document.createElement('my-el');
el.setAttribute('tag-model', "[[myBinding]]");

it creates element without its attributes. How to construct custom element with all its attributes and then append to HTML to initilize them?

Thank you!

Upvotes: 0

Views: 253

Answers (1)

divoom12
divoom12

Reputation: 812

See this

It basically says to do it like that:

var dynamicEl = document.createElement("my-element");
dynamicEl.setAttribute("id", "my-element-id");
dynamicEl.setAttribute("greeting", "Hello, Good Morning.");
document.body.appendChild(dynamicEl);

However, if you want to change properties directly, it wont work with setAttribute. You have to do it like that:

var dynamicEl = document.createElement("my-element");
dynamicEl.greeting = 'Waaazaaaa???';
document.body.appendChild(dynamicEl); 

Upvotes: 1

Related Questions