Last active
August 29, 2015 14:07
-
-
Save manar007/7f5695313919c2ae4463 to your computer and use it in GitHub Desktop.
Find element inside another element with class
This file contains 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
/** | |
* @description finds element by given classname inside the dom list of givent element | |
* NOTE its will return only one element | |
* @param element <HTMLElement> | |
* @param className <String> | |
* @returns {*} <HTMLElement> | |
*/ | |
function getElementByClassName(element, className) { | |
var foundedElement; | |
function findElement(element, className) { | |
var i, | |
length = element.childNodes.length; | |
if (foundedElement) { | |
return; | |
} | |
for (i = 0; i < length; i++) { | |
if (element.childNodes[i].nodeType && element.childNodes[i].nodeType === 1) { | |
if (element.childNodes[i].classList.contains(className)) { | |
foundedElement = element.childNodes[i]; | |
break; | |
} | |
else { | |
findElement(element.childNodes[i], className); | |
} | |
} | |
} | |
} | |
findElement(element, className); | |
return foundedElement; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment