Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
A small library to retrieve DOM elements like jQuery.
;(function(w, d, undefined){
'use strict';
var $ = function (e) {
var firstChar = e.charAt(0);
if (firstChar === '#') {
return d.querySelector(e);
} else if (firstChar === '.') {
return d.querySelectorAll(e);
} else {
return d.getElementById(e);
}
};
// add to window
w.$ = $;
})(window, document);
console.log($('#my-element')); // querySelector()
console.log($('.my-element')); // querySelectorAll()
console.log($('my-element')); // getElementById()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.