Custom Elements

"Enabling the author to define and use new types of DOM elements."

Alternatives

Demo

API

XSquare

                  
                      <x-square></x-square>
                  
            
                  
// Prototype inheritance
var XSquarePrototype = Object.create(HTMLElement.prototype);

XSquarePrototype.createdCallback = function() {
    // Add style
    this.style.border = "red 5px dashed";
    this.style.width = "50px";
    this.style.height = "50px";
};

var XSquare = document.registerElement('x-square', {
    prototype: XSquarePrototype
});
                  
            

Example:

XRectangle

                
                    <x-rectangle></x-rectangle>
                
            
              
// Prototype inheritance
var XRectanglePrototype = Object.create(XSquarePrototype);

XRectanglePrototype.createdCallback = function() {
    // Call base
    XSquarePrototype.createdCallback.call(this);

    // Add width
    this.style.width = "250px";
};

var XRectangle = document.registerElement('x-rectangle', {
    prototype: XRectanglePrototype
});
                  
            

Example:

XSquareButton

                
                    <button is="x-square-button"></button>
                
            
                  
// Prototype inheritance
var XSquareButtonPrototype = Object.create(HTMLButtonElement.prototype);

XSquareButtonPrototype.createdCallback = function() {
    // Add style
    this.style.border = "red 3px dashed";
    this.style.width = "50px";
    this.style.height = "50px";

    // Add click event
    this.onclick = function() { alert("Good job!"); };
    this.innerHTML = "Hi";
};

var XSquare = document.registerElement('x-square-button', {
    prototype: XSquareButtonPrototype,
    extends: 'button'
});
                  
            

Example: