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

Time:02-04

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
                addedNode.body.style.background = "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.

CodePudding user response:

Set Element.style.background:

addedNode.style.background = 'red'
  • Related