Skip to content

Instantly share code, notes, and snippets.

@ram2104
Last active June 25, 2017 02:04
Show Gist options
  • Save ram2104/ca9d8c16e8ed48bed90ab4ec3b30186c to your computer and use it in GitHub Desktop.
Save ram2104/ca9d8c16e8ed48bed90ab4ec3b30186c to your computer and use it in GitHub Desktop.
Implementation getElements pollyfills
var startNode = document.body;
// multipleNode = false
function getElementByType(nodeList, type, value){
for(var outerIdx = 0,outerIdxLen = nodeList.childNodes.length; outerIdx < outerIdxLen; outerIdx++ ){
if(nodeList.childNodes[outerIdx].childNodes.length){
getElementByType(nodeList.childNodes[outerIdx], type, value);
}
if(type == "tagname"){
if(nodeList.childNodes[outerIdx].tagName && nodeList.childNodes[outerIdx].tagName.toLowerCase() == value.toLowerCase()){
console.log(nodeList.childNodes[outerIdx]);
console.log("Found tag Name");
return nodeList.childNodes[outerIdx];
}
} else if(type == "id") {
if(nodeList.childNodes[outerIdx].attributes){
for(var innerIdx= 0, innerIdxLen = nodeList.childNodes[outerIdx].attributes.length; innerIdx < innerIdxLen; innerIdx ++){
if(nodeList.childNodes[outerIdx].attributes[innerIdx].name == "id" && nodeList.childNodes[outerIdx].attributes[innerIdx].value == value){
console.log(nodeList.childNodes[outerIdx]);
console.log("id");
return nodeList.childNodes[outerIdx];
}
}
}
} else if(type == "class"){
if( nodeList.childNodes[outerIdx].classList && nodeList.childNodes[outerIdx].classList.contains(value)){
console.log(nodeList.childNodes[outerIdx]);
console.log("class");
return nodeList.childNodes[outerIdx];
}
} else {
console.log("not supported or not found");
return;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment