12

This example is, for a matter of simplicity, just an illustration of what I need to do. Say, I have an element, which style is set in a CSS. The style is set for the element as well as for a Pseudo-class, say element:hover. I can override the style for the element with the following JavaScript: element.style.setProperty('property-name', 'new value', ''). How can I change the Pseudo-class's style through JavaScript?

Thank you.

<html>
<head>
      <style type='text/css'>
             #myBtn {
                background-color: red;
             }
             #myBtn:hover {
                background-color: blue;
             }
      </style>
</head>
<body>
      <button id='myBtn'>Colored button</button>

      <button onclick="ChangeColor();">Script button</button>

      <script type="application/x-javascript">
              function ChangeColor() {
                 var Btn = document.getElementById('myBtn');
                 Btn.style.setProperty('background-color', 'green', '');
              };
      </script>
</body>
</html>
BoltClock
  • 700,868
  • 160
  • 1,392
  • 1,356
GreenBear
  • 373
  • 1
  • 6
  • 18

4 Answers4

7

I would use a different set of rules with an additional class

<html>
<head>
  <style type='text/css'>
    #myBtn {
      background-color: red;
    }
    #myBtn:hover {
      background-color: blue;
    }
    #myBtn.changed {
      background-color: green;
    }
    #myBtn.changed:hover {
      background-color: yellow; /* or whatever you want here */
    }
</head>

And then

<script type="application/x-javascript">
      function ChangeColor() {
         var btn = document.getElementById('myBtn');
         btn.className = "changed";
      };
</script>

Of course, this makes only sense for one or a couple of different values you want to have for your color.

Björn Landmesser
  • 953
  • 10
  • 26
6

CSS pseudo-classes and pseudo-elements are not part of the DOM, and thus you cannot modify their style properties directly using JavaScript.

Certain dynamic pseudo-classes like :hover may have similar JavaScript events (onmouseover onmouseout) that you can use to change styles, but this doesn't mean there's an actual correlation between the two.

BoltClock
  • 700,868
  • 160
  • 1,392
  • 1,356
  • 1
    Well that's not completely true - JavaScript can certainly create a new ` – Pointy Jun 19 '11 at 21:27
  • Thank you all for the help! I think changing the class is the way to go for my task. Sad that seemingly simple tasks require workarounds. You guys have a good day! – GreenBear Jun 19 '11 at 22:24
4

You can't modify them directly but you can insert the CSS to the head portion of the DOM.

Emil
  • 8,449
  • 3
  • 27
  • 44
2

You can tinker with existing stylesheets in IE6+ and most of the other browsers.

Editing existing rules is possible, but tricky cross-browser.

This example adds a new rule (selector and declaration) to the last stylesheet in the document.

function addCss(sel, css){
    S= document.styleSheets[document.styleSheets.length-1];
    var r= (S.cssRules!= undefined)? S.cssRules: S.rules;
    if(S.insertRule) S.insertRule(sel+'{'+css+'}',r.length);
    else if(S.addRule)S.addRule(sel,css,r.length);
}

 addCss('button:hover','background-color:blue;');
kennebec
  • 102,654
  • 32
  • 106
  • 127
  • Trying to use this I get "The property 'cssRules' does not exist on value of type 'StyleSheet'" and "The property 'insertRule' does not exist on value of type 'StyleSheet'". – HischT Mar 27 '14 at 08:03