Creating my own functions instead of jQuery
I am writing my own functions instead of using jQuery and want to see if
the could I came up with is sufficient enough and if anyone sees any
issues?
function _(x,child) {
var a = document.getElementById(x);
for (var i = 0; i < a.childNodes.length; i++) {
if (a.childNodes[i].className == child) {
return a.childNodes[i];
}
}
if(child === null){
return a;
}
}
I will then have to create a code to be sufficient to work for all major
browsers including IE7-8 for getting an element by className wanted to
ensure if anyone had any better ideas?
EDIT:
This is for practice as I'll start college soon so I want to have a basic
understanding, please don't reference jQuery as I did not tag it.
UPDATE:
Code for easy class selection and Id Class selection:
function _(x,child) {
var a = document.getElementById(x);
if(x !== null && child !==null){
for (var i = 0; i < a.childNodes.length; i++) {
if (a.childNodes[i].className == child) {
return a.childNodes[i];
}
}
}
else if(child === null)
{
return a;
}
else if(x === null)
{
if(document.getElementsByClassName){
return document.getElementsByClassName(child);
}
if(document.querySelectorAll){
return document.querySelectorAll("."+child);
}
}
}
No comments:
Post a Comment