Reputation: 2365
I'm trying to recreate jQuery's id and classname selectors. Here's what I have so far:
function $(id) {
return document.getElementById(id);
}
If I have an element<div id="fire">Fire</div>
, then I can call for it with:
$('fire').style.fontSize = '36px';
This works brilliantly, however, I want to extend this to use id and classname with #
and .
respectively. What I've attempted does not work:
Upvotes: 1
Views: 146
Reputation: 3110
The example in your fiddle doesn't work because it's looking for an element with the ID of "#fire" (with the # character). You need to omit this character:
function $(id) {
if (id.indexOf('#') == 0) {
return document.getElementById(id.substring(1));
}
}
http://jsfiddle.net/6t58Lc7v/1/
Upvotes: 4