jQuery like selector with querySelector
;(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);
}
};
w.$ = $;
})(window, document);
console.log($('#my-element')); // querySelector()
console.log($('.my-element')); // querySelectorAll()
console.log($('my-element')); // getElementById()
Written by Boopathi Rajaa
Related protips
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Selector
Authors
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#