<script>
customElements.define( "my-counter", class extends HTMLElement {
get buttonspan() {
return this.button.querySelector("span");
}
get count() {
return ~~this.buttonspan.textContent;
}
set count(value) {
this.buttonspan.textContent = value;
}
constructor( count = 0 , increment = 1) { // Web Component can be extended with different initial state
// one helper function every Framework also uses
let createElement = (tag,props={}) => Object.assign(document.createElement(tag),props);
super() // sets AND returns 'this' scope
.attachShadow({mode:"open"}) // sets AND returns this.shadowRoot
.append( // not in IE Browser, old geezers use appendChild, append can do more
this.button = createElement("button", {
innerHTML: `Clicked <span>${count}</span> times`, // only change that span!
onclick: e => this.count = count += increment
})
)
}
})
customElements.define("counter-42", class extends customElements.get("my-counter"){
constructor() {
super(42,42)
}
})
</script>
<my-counter></my-counter>
<my-counter></my-counter>
<my-counter></my-counter>
<counter-42></counter-42>
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Fiddle Pages
Your fiddles accessible under a custom domain and a vanity path - the simplest hosting for your personal project.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Get Fiddle Pages with PRO
Your fiddles accessible under a custom domain and a vanity path - the simplest hosting for your personal project.