Skip to content

Instantly share code, notes, and snippets.

@pbojinov
Created February 20, 2013 19:03
Show Gist options
  • Save pbojinov/4998172 to your computer and use it in GitHub Desktop.
Save pbojinov/4998172 to your computer and use it in GitHub Desktop.
jQuery like selector
var $ = function (selector) {
var cl, id, alls, arrEle = [];
if (/^\./.test(selector)) {
cl = selector.replace(".", "");
alls = document.getElementsByTagName("*"), l = alls.length;
for (var i=0; i<l; i+=1) {
var name = alls[i].className, arrName = [];
if (name) {
arrName = name.split(" "), lName = arrName.length;
for (var j=0; j<lName; j+=1) {
if (arrName[j] === cl) {
arrEle.push(alls[i]);
}
}
}
}
} else if (/^#/.test(selector)) {
id = selector.replace("#", "");
var o = document.getElementById(id);
if (o) {
arrEle.push(o);
}
}
return arrEle;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment