Skip to content
Advertisement

Tag: custom-element

attributeChangedCallback() always called twice ending up with multiple event listeners

Playing around with custom elements, I’m trying to get a click event fired depending on custom element attribute’s value. But using the attributeChangedCallback() method (along with the connectedCallback() method) I’m ending up with multiple event listeners. Why is the attributeChangedCallback() method always called twice and therefore adds two event listeners? How to avoid this? What would be best practice? Answer

Why does customElements.upgrade appear to not upgrade this custom element?

I have a situation similar to the example below: from a template element, I clone a node tree containing custom elements. One custom element is passed data during initialization, represented here by the line infobox.setData(getData()). The function I use to pass the data (setData) is added by my custom class, so I make sure the custom element is upgraded before

Initialisation of Custom Elements Inside Document Fragment

Consider this HTML template with two flat x-elements and one nested. How to initialise (fire constructor) all custom elements in cloned from fooTemplate document fragment without appending it to DOM, neither by extending built-in elements with is=”x-element”; either entire fragment. Note that script executes with defer attribute. Answer We can initialise template with this arrow function: Or with this method

Advertisement