Last active
June 25, 2017 02:04
-
-
Save ram2104/ca9d8c16e8ed48bed90ab4ec3b30186c to your computer and use it in GitHub Desktop.
Implementation getElements pollyfills
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 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