Skip to content

Instantly share code, notes, and snippets.

@joside
Created May 24, 2011
Embed
What would you like to do?
Selector function in 140byt.es
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
}
//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
function(i,f,s){f=i[0];s=i.substr(1);return document['getElement'+({'#':'ById','.':'sByClassName'}[f]||'sByTagName')]({'#':s,'.':s}[f]||i)}
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.
{
"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