Here is a function I have written. It works well to get the closest element that has a certain attribute:
// Example of attribute if id
export function getClosestAttribute(
element: SVGElement,
attribute: keyof typeof svgAttributeToCssStyle,
excludeElement: boolean
): string | null {
let firstIncludedEl: Element | null = element;
if (excludeElement) {
firstIncludedEl = element.parentElement;
}
if (!firstIncludedEl) {
return null;
}
const closestEl = firstIncludedEl.closest(`[${attribute}]`);
if (!closestEl) {
return null;
}
return closestEl.getAttribute(attribute);
}
Is it possible to write something similar for styles? E.g. I want to select any elements that have style="...fill:..."?