Created
August 6, 2011 06:47
-
-
Save chrisevans/1129094 to your computer and use it in GitHub Desktop.
Selector function in 140byt.es
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function( | |
i, //the selector parameter | |
f, //placeholder | |
s //placeholder | |
) { | |
f = i[0]; //extract the first character of the selector | |
s = i.substr(1); //extract the remaining part of the selector after a possible '#' or '.' | |
return document['getElement' + ({ //call document.getElement.... we'll complete the method name later with this map which has '#' and '.' as it's keys | |
'#': 'ById', //this handles ID selectors with .getElementById() | |
'.': 'sByClassName' //class selectors are handled with .getElementsByClassName() | |
}[f] || 'sByTagName')]({ //Everything else is handled with .getElementsByTagName() | |
'#': s, | |
'.': s | |
}[f] || i) //we call the method either with the complete selector i (for tag names) or with the shortened one s | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//suppose, the function has been assinged to "$": | |
$('#foo'); //returns one DOM element with the ID "foo" | |
$('.bar'); //returns an array of DOM elements with the class "bar" | |
$(div); //returns an array of all <div> elements |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function(i,f,s){f=i[0];s=i.substr(1);return document['getElement'+({'#':'ById','.':'sByClassName'}[f]||'sByTagName')]({'#':s,'.':s}[f]||i)} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE | |
Version 2, December 2004 | |
Copyright (C) 2011 YOUR_NAME_HERE <YOUR_URL_HERE> | |
Everyone is permitted to copy and distribute verbatim or modified | |
copies of this license document, and changing it is allowed as long | |
as the name is changed. | |
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE | |
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION | |
0. You just DO WHAT THE FUCK YOU WANT TO. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "Selector140", | |
"description": "Accepts a single ID, class or tag name selector and return the matching DOM elements", | |
"keywords": [ | |
"140bytes", | |
"DOM", | |
"selector", | |
"matching" | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment