Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel is required to process package imports. If you need a different preprocessor remove all packages first.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                .elem-container
  select.elem-select
  button.elem-change Change Element

.elem-container
  .element-info
    | Element: 
    span.elem-type
  .element
h2
  a(href="https://developer.mozilla.org/en-US/docs/Web/Events") Events available
  span.events-available-count
ul.event-list

h2 Events
ul.events
div
  span.event-description
  span.events-count  0
  |  events have been logged.
              
            
!

CSS

              
                
.elem-container {
  background-color: #f0f0f0;
  margin: 0;
  padding: 20px;
  text-align: center;
  
  .elem-select {
    
    option {
      text-transform: capitalize;
    }
  }
  
  .elem-type {
    text-transform: capitalize;
  }
  
  .elem {
    border: 1px solid #f00000;
    background-color: #00f000;
    color: #0000f0;
    margin: 0.5rem auto;
    min-height: 1.5rem;
    min-width: 1.5rem;
  }
}

.event-list, .events {
  border: 1px solid black;
  display: block;
  list-style-type: none;
  margin: 0 auto;
  min-height: 1.25em;
  max-height: 12em;
  overflow: auto;
  padding: 0;
  width: 80%;
  
  li {
    font-size: 1rem;
    height: 1rem;
    line-height: 1.25rem;
    
    &:nth-child(even) {
      background-color: #f0f0f0;
    }
  }
}

.event-list {
  
}

.events {
  
}
              
            
!

JS

              
                class EventSystem {
  constructor(element) {
    this._ = {
      element: null
    }
    
    return this;
  }
  
  getAllEventTypes({blacklist = [], whitelist = []} = {}) {
    const events = [];
    for (let property in this._.element) {
      const match = property.match(/^on(.*)/);
      if (match) {
        if ((whitelist.length > 0 ? whitelist.indexOf(match) !== -1 : true) &&
            (blacklist.length > 0 ? blacklist.indexOf(match) === -1 : true)) {
          events.push(match[1]);
        }          
      }
    }
    return events;
  }
  
  getElementType() {
    return this._.element.tagName.toLowerCase();
  }
  
  setElement(element) {
    this._.element = element;
    return this;
  }
  
  applyEvents(events, callback) {
    events.forEach((event) => {
      this._.element.addEventListener(event, (ev) => {
        if (typeof callback === 'function') {
          callback(event, ev);
        }
      })
    })
  }
}
(() => {
  let initialElement = 'button';
  let el = {
    element: document.querySelector('.element'),
    elemSelect: document.querySelector('.elem-select'),
    elemChange: document.querySelector('.elem-change'),
    eventsAvailableCount: document.querySelector('.events-available-count'),
    eventsCount: document.querySelector('.events-count'),
    eventDescription: document.querySelector('.event-description'),
    elemType: document.querySelector('.elem-type'),
    eventList: document.querySelector('.event-list'),
    events: document.querySelector('.events')
  }
  let elems = [
    'a',
    'abbr',
    'address',
    'area',
    'article',
    'aside',
    'audio',
    'b',
    'base',
    'bdi',
    'bdo',
    'blockquote',
    'body',
    'br',
    'button',
    'canvas',
    'caption',
    'cite',
    'code',
    'col',
    'colgroup',
    'data',
    'datalist',
    'dd',
    'del',
    'details',
    'dfn',
    'dialog',
    'div',
    'dl',
    'dt',
    'em',
    'embed',
    'fieldset',
    'figcaption',
    'figure',
    'footer',
    'form',
    'h1',
    'h2',
    'h3',
    'h4',
    'h5',
    'h6',
    'head',
    'header',
    'hgroup',
    'hr',
    'html',
    'i',
    'iframe',
    'img',
    'input',
    'ins',
    'kbd',
    'keygen',
    'label',
    'legend',
    'li',
    'link',
    'main',
    'map',
    'mark',
    'menu',
    'menuitem',
    'meta',
    'meter',
    'nav',
    'noscript',
    'object',
    'ol',
    'optgroup',
    'option',
    'output',
    'p',
    'param',
    'pre',
    'progress',
    'q',
    'rb',
    'rp',
    'rt',
    'rtc',
    'ruby',
    's',
    'samp',
    'script',
    'section',
    'select',
    'small',
    'source',
    'span',
    'strong',
    'style',
    'sub',
    'summary',
    'sup',
    'table',
    'tbody',
    'td',
    'template',
    'textarea',
    'tfoot',
    'th',
    'thead',
    'time',
    'title',
    'tr',
    'track',
    'u',
    'ul',
    'var',
    'video',
    'wbr'
  ];
  let es = new EventSystem();
  let maxEvents = 200;
  
  let emptyElement = (element, max = 0) => {
    while (element.children.length > max) {
      element.removeChild(element.lastChild);
    }
  };

  let loadTag = (e) => {
    console.log(e)
    emptyElement(el.events);
    emptyElement(el.eventList);
    emptyElement(el.element);
    
    el.element.insertAdjacentHTML('beforeend', `<${e.toLowerCase()}>This is a ${e.toLowerCase()} tag.</${e.toLowerCase()}>`);

    let events = es.getAllEventTypes();
    let element = el.element.querySelector(e);
    es.setElement(element);
    
    el.elemType.textContent = e;
    el.eventsAvailableCount.textContent = ' ' + events.length;
    element.classList.add('elem');
    
    events.sort().forEach((eventName) => {
      el.eventList.insertAdjacentHTML('beforeend', `<li><a href="https://developer.mozilla.org/en-US/docs/Web/Events/${eventName}" target="_blank">${eventName}</a></li>`);
    });

    es.applyEvents(events, (eventName, event) => {
      event.stopPropagation();
      let stamp = new Date(Date.now()).toLocaleString();
      el.eventsCount.textContent = ' ' + el.events.children.length;
      el.events.insertAdjacentHTML('afterbegin', `<li>${stamp}: <a href="https://developer.mozilla.org/en-US/docs/Web/Events/${eventName}" target="_blank">${eventName}</a></li>`);
    });
  };

  elems.sort().forEach((elm) => {
    el.elemSelect.insertAdjacentHTML('beforeend', `<option value="${elm.toLowerCase()}">${elm}</option>`);
  });
  
  el.elemChange.addEventListener('click', () => {
    let e = el.elemSelect.value;
    
    emptyElement(el.events, maxEvents);
    loadTag(e);
  });
  
  el.eventDescription.textContent = `Only a maximum of ${maxEvents} will be logged.`;
  el.elemSelect.querySelector(`option[value="${initialElement}"]`).setAttribute('selected', '');
  
  emptyElement(el.events);
  setTimeout(() => {
    loadTag(initialElement); // NOTE: why does this not work with just one?
    loadTag(initialElement);
  }, 10);
})();
              
            
!
999px

Console