I have a data object that contains data to find a specific element that exist within a DOM Tree and after I find that element I want to change it css by adding inline css.
{
outerHtml: '<a class="btn btn-pill">\n <svg xmlns="http://www.w3.org/2000/svg" class="icon" width="24" height="24" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round"><path stroke="none" d="M0 0h24v24H0z" fill="none"></path>',
selector: ".col-auto.ml-auto.d-print-none>.d-flex>.btn.btn-pill",
style: `background-color: #206bc4; color: white; svg { color: white }`,
}
I'm utilizing this data and injecting the style data in to the element once I find it. As you can see in my outerHTML, the parent prop also has a child and in my style I have the css to change the svg color as well. But when using my code below. It only changes the style of the a tag as opposed to the svg as well.
Note: the outerHTML & selector are just for finding the correct element to change. Sort of like unique identifiers
All I'm doing in the below code is using my selectors to find the correct element and then add the CSS to the final node.
export function findAndInject(remove) {
const data = getData();
let selector = data.selector.split(',');
let select;
for (const prop in selector) {
if (prop == 0) {
select = document.querySelector(selector[prop]);
} else {
select = select.querySelector(selector[prop]);
}
}
if (remove) {
select.style.cssText = null;
} else {
select.style.cssText = data.style;
}
}
I know the issue is probably that I'm doing style.cssText
but is there any other way I can add an inline style that can also be added to the child without creating a stylesheet or using style tags?
The Plus icon should be white.