Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save manar007/7f5695313919c2ae4463 to your computer and use it in GitHub Desktop.
Save manar007/7f5695313919c2ae4463 to your computer and use it in GitHub Desktop.
Find element inside another element with class
/**
* @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