cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - ActiveLovehtmlicon-new-collectionicon-personicon-teamlog-outoctocatpop-outspinnerstartv

Pen Settings

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.

Quick-add: + add another resource

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.

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.

            
              <div id="container">
    <h1>jQuery like selection helper</h1>
</div>
            
          
!
            
              body {
  font: 12px Arial;
}

#container {
  padding: 10px;
  border: 1px solid black;
  width: 300px;
  margin: 20px auto;
}

h1 {
  text-align: center;
}

ul {
  margin: 0;
  padding: 0;
  list-style: none;
  text-align: center;
}

li {
  padding: 5px;
}
            
          
!
            
              /*
 * jQuery like selection helper
 *
 * Waits for a selector, HTML or DOM elements
 * Second argument is an optional context
 * 
 * Returns an array
 */

function $ (selector, context) {
  if (!selector) {
    return [];
  }
  
  // If selector is a function
  else if (typeof selector === 'function') {
    return document.addEventListener("DOMContentLoaded", selector);
  }

   // If selector it's an array we assume it's an array of elements
  else if (selector instanceof Array) {
    return selector;
  }
  
  // If selector DOM element
  else if (selector.nodeType === 1) {
    return [ selector ];
  }
  
  // If selector is HTML
  else if (selector.match(/^\s*<(\w+|!)[^>]*>/)) {
    var container;
    
    container = document.createElement('div');
    container.innerHTML = selector;

    return [].slice.call(container.childNodes);
  }
  
  // Otherwise we assume it's a string selector
  else {
    if (context) {
      context = $(context)[0];
    } else {
      context = document;   
    }
    
    return [].slice.call(context.querySelectorAll(selector));
  }
};


/*
 * Now, let's test it
 * When DOM Content is loaded
 */

$(function () {
  /*
   * ...and create DOM elements from HTML
   */
  var html;
  html  = '<ul class="list">';
  html += '  <li>1st row</li>';
  html += '  <li>2d row</li>';
  html += '  <li>3rd row</li>';
  html += '  <li>4th row</li>';
  html += '</ul>';
  
  $(html).forEach(function (item) {
     $('#container')[0].appendChild(item);
  });

  /*
   * Let's select some LIs
   */
  $('li', '.list').forEach(function (item, idx) {
      item.style.cursor = 'pointer';
      item.onclick = function () {
        alert('item ' + idx);
      }
  });

  $('ul li:nth-child(odd)').forEach(function (item) {
      item.style.backgroundColor = '#eee';
  });

});

            
          
!
999px
Loading ..................

Console