Jan. 26, 2024, 10:57 a.m. | Danny Engelman

DEV Community dev.to

How would you address your new found love?



  class ClickButton extends HTMLElement {
constructor() {
super(); // Always call super() first
this.attachShadow({mode:'open'});
const button = document.createElement('button');
button.innerHTML = 'Click me';
this.shadowRoot.appendChild(button);
button.addEventListener('click', () => {
alert('Button clicked!');
});
}
}
customElements.define('click-button', ClickButton);





or


  customElements.define('click-button', class extends HTMLElement {
constructor() {
const createElement = (tag, props = {}) => Object.assign(document.createElement(tag), props);
super().attachShadow({mode:'open'})
.append(
createElement( 'button', {
innerHTML: 'Click me',
onclick : () => alert('Button clicked!')
})
);
}
});





JSFiddle playground: …

alert call class click document dom found frontend javascript love tag web webcomponents

Software Engineer for AI Training Data (School Specific)

@ G2i Inc | Remote

Software Engineer for AI Training Data (Python)

@ G2i Inc | Remote

Software Engineer for AI Training Data (Tier 2)

@ G2i Inc | Remote

Data Engineer

@ Lemon.io | Remote: Europe, LATAM, Canada, UK, Asia, Oceania

Artificial Intelligence – Bioinformatic Expert

@ University of Texas Medical Branch | Galveston, TX

Lead Developer (AI)

@ Cere Network | San Francisco, US