I try to get a html custom element to work:
When viewing the following example in the browser I must conclude that this.dataset.text attribute in the constructor() is:
- present before the script defining the custom element
- empty after the script defining the custom element
<html>
<head>
<title>myimage</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<div>
before custom element definition: <my-element data-text="Replaced and works as expected">Not Replaced</my-element>
</div>
<script>
class MyElement extends HTMLElement {
constructor() {
super();
this.text_attribute= this.dataset.text;
}
connectedCallback() {
if (this.text_attribute) {
this.innerText = this.text_attribute;
}
}
}
customElements.define('my-element', MyElement);
</script>
<div>
after custom element definition: <my-element data-text="Replaced">Not Replaced</my-element>
</div>
</body>
</html>
This seems to be a common behavior in chrome and Firefox. I can provide some more facts:
- the
this.dataset.textis always present inconnectedCallback() - accessing the
text-attribut viathis.getAttribute('data-text')does behave in the same way as above
Can anyone explain the purpose of this seemingly buggy behavior?