function findAncestor (el, cls) {
while ((el = el.parentElement) && !el.classList.contains(cls));
return el;
}
function getParent(elemSelector, parentSelector) {
var elem = document.querySelector(elemSelector);
var parents = document.querySelectorAll(parentSelector);
for (var i = 0; i < parents.length; i++) {
var parent = parents[i];
if (parent.contains(elem)) {
return parent;
}
}
return null;
}
var result = getParent(
'.product__shopcard-btn',
'.product'
);
console.log(result);
document.querySelector(".product__shopcard-btn").closest(".product"); // <div class="product"></div>
let element = document.querySelector(".product__shopcard-btn");
while (!element.classList.contains("product")) {
element = element.parentElement;
if (!element) {
break;
}
}
const find = (node, className) => {
while (node) {
if (node.classList.contains(className)) {
return node;
} else {
node = node.parentElement;
}
}
return null;
}
if (elem.closest('.ptoduct')) {
// выполняем функцию elem...
}