I have the following code to set the opacity of all elements not being hovered over to 0.5.
const handleHover = function (e) {
if ($(e.target).hasClass("nav_link")) {
const siblings = e.target.closest("nav").querySelectorAll(".nav_link");
siblings.forEach((el) => {
if (el !== e.target) el.style.opacity = this;
});
}
};
$(".nav").on("mouseover", handleHover.bind(0.5));
$(".nav").on("mouseout", handleHover.bind(1));
My question is how can I write the forEach loop on the siblings using a jQuery method rather than using querySelectorAll?
I wouldn't use JavaScript or jQuery at all for this.
Using CSS, you can target all the
.nav_linkelements that are within anavbeing hovered but are themselves, not hovered.For completeness, here's a jQuery version