Course contentsShow
← Web Development
Lesson 2453 of 2,98244. Web ComponentsFree lesson

Extending Built-in Elements

Creating customized built-in elements by extending native elements like HTMLButtonElement instead of HTMLElement.

Extending Built-in Elements

You'll learn how to create customized built-in elements by extending native HTML elements instead of generic HTMLElement.


So far you've defined custom elements by extending HTMLElement, creating completely new tags from scratch. But what if you want to enhance an existing button, input, or paragraph with custom behavior while keeping all its native functionality and accessibility features intact?

This is where customized built-in elements come in. Instead of extending HTMLElement, you extend a specific built-in element class like HTMLButtonElement, HTMLInputElement, or HTMLParagraphElement. Your custom element inherits all the native element's properties, methods, and built-in behaviors.

// Extend a native button instead of HTMLElement
class FancyButton extends HTMLButtonElement {
  connectedCallback() {
    this.addEventListener('click', () => {
      this.classList.add('clicked');
    });
  }
}

// Register with the 'extends' option
customElements.define('fancy-button', FancyButton, { extends: 'button' });

Notice two key differences:

  1. Class declaration: You extend HTMLButtonElement instead of HTMLElement
  2. Registration: You pass a third argument { extends: 'button' } to tell the browser which native element you're customizing

To use it in HTML, you don't create a new tag. Instead, you use the native tag with the is attribute:

<button is="fancy-button">Click me</button>

Why do this? You get all the native element's functionality for free—form submission, focus management, ARIA support, keyboard navigation—plus your custom enhancements. It's like putting a turbocharger on an existing car instead of building one from scratch.

Key Takeaway: Extending built-in elements like HTMLButtonElement creates customized versions of native tags that preserve all built-in functionality while adding your custom behavior—registered with the extends option and used via the is attribute.