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 includes JSX processing.

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

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

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

              
                <div id="toolbox">
  <fieldset>
    <legend>Add Event</legend>
    <select id="eventlist">
      <option value="click">Click</option>
      <option value="focusin">Focus In</option>
      <option value="focusout">Focus Out</option>
      <option value="mouseover">Mouse Over</option>
    </select>
    <button id="addevent" onclick="addEvent();">Add Event</button>
  </fieldset>
  <fieldset>
    <legend>Remove Event</legend>
    <select id="activeeventlist">

    </select>
    <button id="removeevent" onclick="removeEvent();">Remove Event</button>
  </fieldset>
</div>

<button id="test"> Test Button </button>
<div id="eventlog">
  <h3>Action Log</h3>
</div>
              
            
!

CSS

              
                div#toolbox{
  width:250px;
  float:right;
  background-color:lightgreen;
}
              
            
!

JS

              
                var eventHandler = {
  events: [], //Array of events the element is subscribed to.

  bindEvent: function(event, callback, targetElement) {
    //remove any duplicate event 
    this.unbindEvent(event, targetElement);

    //bind event listener to DOM element
    targetElement.addEventListener(event, callback, false);

    this.events.push({
      type: event,
      event: callback,
      target: targetElement
    }); //push the new event into our events array.
  },

  findEvent: function(event) {
    return this.events.filter(function(evt) {
      return (evt.type === event); //if event type is a match return
    }, event)[0];
  },
  unbindEvent: function(event, targetElement) {
    //search events
    var foundEvent = this.findEvent(event);

    //remove event listener if found
    if (foundEvent !== undefined) {
      targetElement.removeEventListener(event, foundEvent.event, false);
    }

    //update the events array
    this.events = this.events.filter(function(evt) {
      return (evt.type !== event);
    }, event);
  }
};

var testBtn = document.getElementById('test');
var log = document.getElementById('eventlog');

function logMsg(e) {
  if (e.type !== undefined) {
    log.innerHTML = log.innerHTML + '<br> Event triggered: ' + e.type;
  }
}

function addEvent() {
  var list = document.getElementById("eventlist");
  var selected = list.options[list.selectedIndex].value;
  eventHandler.bindEvent(selected, logMsg, document.getElementById('test'));
  activeList();
}

function activeList() {
  var list = document.getElementById('activeeventlist');
  list.innerHTML = '';
  eventHandler.events.forEach(function(e) {
    var opt = document.createElement('option');
    opt.setAttribute('value', e.type);
    opt.innerHTML = e.type;
    list.appendChild(opt);
  });
}

function removeEvent() {
  var list = document.getElementById("activeeventlist");
  var selected = list.options[list.selectedIndex].value;
  eventHandler.unbindEvent(selected, document.getElementById('test'));
  activeList();
}
              
            
!
999px

Console