Get the parents of the element, optionally filtered by a selector.
npm install --save dom-parents
import getParents from 'dom-parents';
getParents(document.querySelector('#main'), '.cat').forEach((element) => {
element.style.backgroundColor = '#008800';
})
import getParents from 'dom-parents';
document.querySelectorAll('.animal').forEach((element) => {
element.addEventListener('mousedown', () => {
const isBobAnAnimal = getParents(this, '.animals').length !== 0;
if (isBobAnAnimal) {
console.log('Bob is animal');
} else {
console.log('Bob is spy!');
}
});
});
import getParents from 'dom-parents';
document.addEventListener('mousedown', (event) => {
const [item] = getParents(event.target, '.item', true);
if (item) {
console.log('mousedown on .item element');
}
});
Returns the parents of the element, optionally filtered by a selector.
Type: object
The element from which the search should start.
Type: string
Selector to search for the parent elements.
Type: bool
Default: false
Include element
to the search or not.