Add style into node that I don't have the id or class

const targetNode = document.getElementById('myElement');

let observer = new MutationObserver(mutations => {
    for(let mutation of mutations) {
        for(let addedNode of mutation.addedNodes) {
            if (addedNode.nodeName === "LI") {
                // other checks that this specific node may pass, but others won't

                addedNode.setAtribute("style", "background: red"); // doesn't work
       = "red"; // doesn't work

observer.observe(targetNode, { childList: true, subtree: true });

How can I set the style to the node? Getting the node’s id or class won’t work because it’s repeated in all added nodes, so I couldn’t find the exact LI element that I want to change.

>Solution :

Set = 'red'

Leave a Reply