Let’s say that I have two span elements. Class is changing width "selected" based on which one is selected. I just need to know when span2 is selected or not.
<span class="swatch swatch-image span1">
<span class="swatch swatch-image span2 selected">
I’m trying to listen which one is selected (which one has class "selected") and then show or don’t show div in other place. Is it possible?
I’ve tried this way:
let targetNode = document.getElementsByClassName('swatch swatch-image span2 selected');
const config = { attributes: true, childList: false, subtree: false, attributeFilter: ['class'] };
const callback = function(mutationsList, observer) {
for (let mutation of mutationsList) {
if (mutation.attributeName === "class") {
var classList = mutation.target.className;
if(/red/.exec(classList).length > 0) {
console.log('Found match');
}
}
}
};
const observer = new MutationObserver(callback);
observer.observe(targetNode, config);
observer.disconnect();
Is MutationObserver is proper way to listen which span is selected? I’m not familiar with javascript, do I need to call MutationObserver somehow after constructing code?
2
Answers
Alright, @mplungjan answer works like a charm. It's really nice and clean code. Thank you. But let's say I was able to add id of div which is parent of span.swatch but I need to find nearest of a div which I was trying to hide/show because I need to show and hide whole tr element:
Should I use:
It's not working because of syntax I think...
And more... display.block is not really want I'm looking for. I would like to delete style.display = "none" at all from tr element
Find the closest container enclosing the spans and delegate