Created
May 15, 2015 18:18
-
-
Save cyzanfar/f1663db79302c1c54a4b to your computer and use it in GitHub Desktop.
This file contains hidden or 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
var $ = function (selector) { | |
var elements = []; | |
var classPosition = selector.split('').indexOf("."); | |
var idPosition = selector.split('').indexOf("#"); | |
var selectorArray = selector.split(""); | |
if ( classPosition === -1 && idPosition === -1){ | |
var elementCollections = document.getElementsByTagName(selector); | |
for (var i = 0; i < elementCollections.length; i++) { | |
elements.push(elementCollections[i]) | |
} | |
}else if (classPosition > -1 && idPosition === -1 ){ | |
classPosition === 0 ? elements.push(document.getElementsByClassName(selector)) : elements.push(document.getElementsByClassName(selector)[1]); | |
console.log('juste une class'); | |
}else if ( idPosition > -1 && classPosition === -1){ | |
if (idPosition === 0) { | |
selectorArray.shift(); | |
elements.push(document.getElementById(selectorArray.join(""))) | |
}else { | |
var tagName = selectorArray.slice(0,idPosition).join("") | |
debugger | |
elements.push(document.getElementsByTagName(tagName)); | |
} | |
console.log('juste une id'); | |
}else if (idPosition > -1 && classPosition > -1){ | |
console.log('class and ID'); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment