Skip to content
Advertisement

Change :hover CSS properties with JavaScript

How can JavaScript change CSS :hover properties?

For example:

HTML

JavaScript

CSS

JavaScript

How can the td :hover properties be modified to, say, background:#00ff00, with JavaScript? I know I could access the style background property using JavaScript with:

JavaScript

But I don’t know of a .style JavaScript equivalent for :hover.

Advertisement

Answer

Pseudo classes like :hover never refer to an element, but to any element that satisfies the conditions of the stylesheet rule. You need to edit the stylesheet rule, append a new rule, or add a new stylesheet that includes the new :hover rule.

JavaScript
Advertisement