1

I have a code like this:

const myDiv = document.querySelector("#myDiv");
myDiv.addEventListener("click", fct_Div);

function fct_Div(ev) {
    console.log(ev);
    if (ev.altKey === true) {
        myDiv.style["background-color"] = "blue";
    }
}

with CSS:

        #myDiv {
            width: 400px;
            height: 400px;
            border: 1px solid black;
        }
        #myDiv:hover {
            background-color: lightblue;
        }

I would like to access #myDiv:hover to change the background color.

Surely there is a way to do that?!

Thanks for your time,

Andreï

0

1 Answer 1

0

you can do like:

#myDiv {
        width: 400px;
        height: 400px;
        border: 1px solid black;#myDiv {
        --hover_background_color: lightblue;
    }

    }
    #myDiv:hover {
        background-color: var(--hover_background_color);
    }

then on js

document.querySelector("#myDiv").style.setProperty('--hover_background_color', "blue");
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.