cssAudio - ActiveCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - Activehtmlicon-personicon-teamoctocatspinnerstartv

Pen Settings

CSS Base

Vendor Prefixing

Add External CSS

These stylesheets will be added in this order and before the code you write in the CSS editor. You can also add another Pen here, and it will pull the CSS from it. Try typing "font" or "ribbon" below.

Quick-add: + add another resource

Add External JavaScript

These scripts will run in this order and before the code in the JavaScript editor. You can also link to another Pen here, and it will run the JavaScript from it. Also try typing the name of any popular library.

Quick-add: + add another resource

Code Indentation

     

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.

            
              <h1>Basic BehaviourController Demo</h1>

<h2>Clock</h2>
<div class="component">
    <div data-behaviour="Clock">Clock is inactive</div>
</div>

<h2>ClearField</h2>
<div class="component">
    <input type="text" data-behaviour="ClearField">
</div>
            
          
!
            
              html {
  background:#fff;
  line-height:1.5;
  padding:2em;
  color:#999;
  overflow:scroll;
}

html,input,textarea,button {
  font-family:Helvetica,Verdana,sans-serif;
  font-size:1em;
}

h1 {
  font-size:1.5em;
  color:#333;
}

h2 {
  margin-top:1.5em;
  font-size:1.25em;
  color:#333;
}

h2+p {
  margin-top:-.75em;
}

p {
  max-width:30em;
}

.component {
  padding:1em;
  border:1px dotted #ccc;
}

            
          
!
            
              /*
 * Clock Class
 */
var Clock = function(element) {

    // set element reference
    this._element = element;

    // start ticking
    this.tick();
};

// Update time
Clock.prototype.tick = function() {
    this._element.textContent = new Date().toString();
    var self = this;
    this._timer = setTimeout(function(){
        self.tick();
    },1000);
};


/*
 * ClearField Class
 */
var ClearField = function(element) {

    // set element reference
    this._element = element;

    // Add clear button
    var clearButton = document.createElement('button');
    clearButton.textContent = 'clear';
    clearButton.addEventListener('click',this);
    this._element.parentNode.insertBefore(clearButton,this._element);
};

// Handle events
ClearField.prototype.handleEvent = function(e) {
    // if event type is click clear the field
    if (e.type === 'click') {
        this._element.value = '';
    }
};


/*
 * BehaviourController Static Class
 */
var BehaviourController = {

    applyDefault:function() {

        document.addEventListener('DOMContentLoaded',function(){
            BehaviourController.applyBehaviour(document);
        });

    },

    // Applies behaviour to given context
    applyBehaviour:function(context) {

        var behaviourConstructor,nodes,i,l;

        nodes = context.querySelectorAll('[data-behaviour]');
        l = nodes.length;

        for (i=0;i<l;i++) {
            behaviourConstructor = nodes[i].getAttribute('data-behaviour');
            new window[behaviourConstructor](nodes[i]);
        }
    }

};



// Run
BehaviourController.applyDefault();
            
          
!
999px
Close

Asset uploading is a PRO feature.

As a PRO member, you can drag-and-drop upload files here to use as resources. Images, Libraries, JSON data... anything you want. You can even edit them anytime, like any other code on CodePen.

Go PRO

Loading ..................

Console