all AI news
Dear Web Component
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
More from dev.to / DEV Community
Jobs in AI, ML, Big Data
Founding AI Engineer, Agents
@ Occam AI | New York
AI Engineer Intern, Agents
@ Occam AI | US
AI Research Scientist
@ Vara | Berlin, Germany and Remote
Data Architect
@ University of Texas at Austin | Austin, TX
Data ETL Engineer
@ University of Texas at Austin | Austin, TX
Business Intelligence Architect - Specialist
@ Eastman | Hyderabad, IN, 500 008