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 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.

            
              <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