Check if an element is a child of another element with a given class name

/** * Check if an element is a child of another element with a given class name * @param {HTMLElement} element Element * @param {String} className Class name * @returns {Boolean} True if element */ const elementIsAParentFrom = (element, className) => { let foundElement = false; if (element && element.classList.length !== 0) { if (element && element.classList) { for ( let node = element; node.classList.contains(className) !== true; node = node.parentNode ) { foundElement = true; } } } if (foundElement !== true) { return false; } return true; };

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.