javascripthtmlcustom-attribute

Create custom tags/attributes using JavaScript


I need to create custom tags inside of HTML. From what I have read on w3schools, you can use <!ATTLIST myattribute type CDATA> to allow the document to remain valid. Would it be possible to add this tag in using JavaScript?


Solution

  • Why yes, yes you can. In fact, they're a stable part of the web components standard.

    Custom Elements is a capability for creating your own custom HTML elements. They can have their own scripted behavior and CSS styling. They are a part of web components, but can also be used by themselves.

    It may be unclear as to why the new custom elements capability was created, as it was already possible to create a tag name like <mytag> and style it with CSS, then use scripting to attach behaviors to it. An advantage that custom elements have are their lifecycle reactions, which allow attaching behaviors to different parts of the new element's "lifecycle." For example, a certain behavior can be attached for when the element is inserted into the DOM ("connected"), and a different behavior when it is removed from the DOM ("disconnected"), or when its attributes change.

    Example

    // Create a class for the element
    class XProduct extends HTMLElement {
      constructor() {
        // Always call super first in constructor
        super();
    
        // Create a shadow root
        var shadow = this.attachShadow({mode: 'open'});
    
        // Create a standard img element and set its attributes.
        var img = document.createElement('img');
        img.alt = this.getAttribute('data-name');
        img.src = this.getAttribute('data-img');
        img.width = '150';
        img.height = '150';
        img.className = 'product-img';
    
        // Add the image to the shadow root.
        shadow.appendChild(img);
    
        // Add an event listener to the image.
        img.addEventListener('click', () => {
          window.location = this.getAttribute('data-url');
        });
    
        // Create a link to the product.
        var link = document.createElement('a');
        link.innerText = this.getAttribute('data-name');
        link.href = this.getAttribute('data-url');
        link.className = 'product-name';
    
        // Add the link to the shadow root.
        shadow.appendChild(link);
      }
    }
    
    // Define the new element
    customElements.define('x-product', XProduct);
    body {
      background: #F7F7F7;
    }
    
    x-product {
      display: inline-block;
      float: left;
      margin: 0.5em;
      border-radius: 3px;
      background: #FFF;
      box-shadow: 0 1px 3px rgba(0,0,0,0.25);
      font-family: Helvetica, arial, sans-serif;
      -webkit-font-smoothing: antialiased;
    }
    
    x-product::slotted(.product-img) {
      cursor: pointer;
      background: #FFF;
      margin: 0.5em;
    }
    
    x-product::slotted(.product-name) {
      display: block;
      text-align: center;
      text-decoration: none;
      color: #08C;
      border-top: 1px solid #EEE;
      font-weight: bold;
      padding: 0.75em 0;
    }
    If nothing appeared below, then your browser does not support Custom Elements yet.
    <x-product data-name="Ruby" data-img="https://s3-us-west-2.amazonaws.com/s.cdpn.io/4621/ruby.png" data-url="http://example.com/1"></x-product>
    <x-product data-name="JavaScript" data-img="https://s3-us-west-2.amazonaws.com/s.cdpn.io/4621/javascript.png" data-url="http://example.com/2"></x-product>
    <x-product data-name="Python" data-img="https://s3-us-west-2.amazonaws.com/s.cdpn.io/4621/python.png" data-url="http://example.com/3"></x-product>