iOS engineer who likes clean, simple designs and sans-serif.

Loading

Javascript, not jQuery [Selectors]

I decided that I’m going to start learning the raw Javascript behind jQuery and post my cheat-sheets here so I can refer to them quickly if I forget. We’ll start out simple with selectors:

// jQuery
$(".myClass");
$('#myID');
$('div');
// Javascript via getElementsBy
document.getElementsByClassName('myClass');
document.getElementsByID('myID');
document.getElementsByTagName('div');
// Javascript via querySelector
document.querySelector('.myClass');
document.querySelectorAll('.myClass');
document.querySelector('#myID');
document.querySelectorAll('div');
document.querySelector('ul li:first-child');
document.querySelectorAll('[data-example]');

var $ = function ( elem ) {
    return document.querySelectorAll( elem );
}
var myClass = $('.myClass');

Leave a Comment