I'm really kind of struggling at the moment and I don't know how hard it can possibly be. I followed several different tutorials and answers here on SO and YouTube, but all are doing it differently and I am kind of stuck right now.
All I want is a custom HTML-Tag with some custom attributes
I tried it with this approach (JS)
class MyTrophy extends HTMLElement {
constructor() {
super();
var rarity = this.getAttribute("rarity");
console.log("rarity: " + rarity);
}
}
customElements.define("my-trophy", MyTrophy);
HTML
<body>
<my-trophy rarity="silver" id="someID">lorem ipsum</my-trophy>
</body>
The JS-File is loaded in the header of the HTML-File
Since it is not an object as well, I cannot log the my-trophy tag as well. It is planned, that I have multiple tags of these kind in my final dom.
It doesn't matter what I try, I don't get the rarity-attribute to work. Am I supposed to just use data-attributes for that case, even though this here is a custom HTML tag?
References:
- https://www.youtube.com/watch?v=jNbOarODRzE
- How can i allow custom tags and attributes html?
- How to create custom tags for html
- https://www.html5rocks.com/en/tutorials/webcomponents/customelements/
Some of the methods simply don't work (anymore?) (for example, it seems like registerElement isn't available anymore, but there is defineElement now).
I would expect, if I log console.log(rarity), that the output would be "silver".
For the completness of the question: I'm trying to run the script in Google Chrome
Thanks for any help