Adding multiple CSS styles to HTML elements using JavaScript
const addStyles = (el, styles) => Object.assign(el.style, styles);
addStyles(document.getElementById('my_id'), {
background: 'red',
color: '#ffff00',
fontSize: '3rem'
});
To get all the ancestors of the element in Dom
const getAncestors = el => {
let ancestors = [];
while (el) {
ancestors.unshift(el);
el = el.parentNode;
}
return ancestors;
};
getAncestors(document.querySelector('nav'));
To find the closest ascending attribute value for the element.
function findUpEle(el, attr) {
while (el.parentNode) {
el = el.parentNode;
if (el[attr]) {
return el;
}
}
return null;
};
document.body.onclick = function (event) {
var closestElearttr = findUpEle(event.target, 'href');
console.log(closestElearttr)
}