I'm trying to create a polyfill of sorts using the Custom Elements API for custom elements used by an in-game browser engine to display buttons and similar. However, I can't seem to access the element's attributes (eg. src, href ...) from within the constructor.
Here is an example:
class KWButton extends HTMLElement {
constructor() {
super();
var attributes = this.attributes;
var shadow = this.attachShadow({
mode: 'open'
});
var img = document.createElement('img');
img.alt = this.getAttribute('text'); // the getAttribute call returns null
img.src = this.getAttribute('src'); // as does this one
img.width = this.getAttribute('width'); // and this
img.height = this.getAttribute('height'); // and this
img.className = 'vivacity-kwbutton'; // this works fine
shadow.appendChild(img);
img.addEventListener('click', () => {
window.location = this.getAttribute('href'); // this works perfectly fine
});
}
}
customElements.define('kw-button',
KWButton);
<kw-button src="https://placekitten.com/g/198/39" width="198" height="39" icon_src="https://placekitten.com/g/34/32" icon_width="34" icon_height="32" href="https://placekitten.com/" text="placekiten" color="#ffffff" size="18"></kw-button>
connectedCallback
lifecycle event. – Gnarled