How to apply style to multiple classes on hover of an element?

JoshuaLC

Whenever I hover over #Rock div I want it to apply a style to both .RockToScissors & .RockToLizard at the same. A the moment I have:

<div onmouseover="overRock()" onmouseout="outRock()">
    <svg>
        <path stroke="#000" fill="#000"/>
    </svg>
</div>

Then in Javascript I have:

function overRock() {
    var myPara = document.querySelector(".RockToScissors");
    myPara.style.stroke = "#008000";
    myPara.style.fill = "#008000";
}

function outRock() {
    var myPara = document.querySelector(".RockToScissors");
    myPara.style.stroke = "#000";
    myPara.style.fill = "#000";
}

But this only allows me to apply the styling to one class. How can I make it apply to both .RockToScissors & .RockToLizard at the same time when hovering #Rock div?

I've tried var myPara = document.querySelectorAll(".RockToScissors, .RockToLizard"); but then the style doesn't apply to either.

Mamun

Include both the class as part of the selector in querySelectorAll(). Then loop through them to set the style individually:

Try the following way:

function overRock() {
  var myPara = document.querySelectorAll(".RockToScissors, .RockToLizard");
  myPara.forEach(function(el){
    el.style.stroke = "#008000";
    el.style.fill = "#008000";
  });
}

function outRock() {
  var myPara = .querySelectorAll(".RockToScissors, .RockToLizard");
  myPara.forEach(function(el){
    el.style.stroke = "#000";
    el.style.fill = "#000";
  });

}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to apply a style to a sibling element from another element

How To Apply Inline Style and className to Same Element

How to style image inside multiple classes

How to toggle style of element both on hover and on click?

how do you add multiple hover on classes

Apply hover effect on child element

How to use nth-child with :hover to style multiple children?

How can I get element:hover style?

how to set style for multiple id element javascript

How to apply jQuery to .element:before:hover?

DIV has multiple classes assigned, but I want to apply a style to only one of those classes, is it possible?

How to apply css hover on multiple elements

Change style of multiple element groups simultaneously on hover using Angular

How to Style Specific Element On Hover With CSS?

How can I apply the ':hover' element styles to the '::after' element?

How to set an element style :focus without style :hover

How can I hover on an element and change the style of far ancestor?

How do apply a CSS pseudo element to elements matching multiple classes?

React SVG style element on hover

how to make apply multiple elements on hover css

How to apply CSS on child element when hover parent element?

Apply style on scrollable element

How to apply bootstrap style to inline <select> element

how to change style on multiple classes after creating the classes?

How to apply style on first child of multilevel recursive classes

How to apply style to child of element with active state

How to apply style for classes

How to apply multiple classes to components on React 5?

how to give multiple classes a style with js