11

Is this possible?

For example, if a user presses the "return" key and I trigger the "mousedown" event, how do I also render the element with :active styles?

I know it is possible to do this using classes, but I'd greatly prefer to use the pre-existing :active styles.

Evan Sharp
  • 2,049
  • 3
  • 12
  • 7
  • This question has been asked many times. http://stackoverflow.com/questions/311052/setting-css-pseudo-class-rules-from-javascript – Casey Robinson Nov 12 '11 at 01:40
  • 6
    @Casey-I don't think this is the same question that you posted a link for. In that link the question was basically how to set a pseudoclass by javascript. Here, Evan seems to be asking how make sure a pseudoclass that is already set in CSS is actually activated based off a javascript event. In other words, he has `:active` styles preset, he is triggering events in javascript, how does he make sure those events are causing the item to be considered "active." – ScottS Nov 12 '11 at 02:18
  • Thanks @Scott, you're correct - I'm asking if you can activate the styles for a CSS pseudo-class using JavaScript, not the other way around. – Evan Sharp Nov 17 '11 at 20:04
  • And the short answer is: that isn't possible. – Nickolay Sep 16 '19 at 00:22

1 Answers1

4

According to the CSS 2.1 spec, the :active pseudo-class applies while:

an element is being activated by the user. For example, between the times the user presses the mouse button and releases it.

You should be able to dispatch a mousedown event with the subject element as the event target and it should stay active until a matching mouseup event is dispatched. If it works, it likely won't work reliably on enough browsers to make it useful.

It would be much simpler (and more widely supported) to add/remove a suitable class.

Edit

Here is an example of using DOMActivate. You can see that dispatching an activate event on an element fires the associated onactivate listener, but doesn't change the appearance of the element being activated.

Perhaps you can simulate actiation by listening for the activate event, adding a class to highlight the element, then remove it after a few moments using setTimeout or similar.

<style type="text/css">
  p:active {
    background-color: red;
  }
  div:active {
    background-color: green;
  }
</style>

<script type="text/javascript">
  function Init () {
    var p, ps = document.getElementsByTagName('p');
    var d = document.getElementById('div0');

    if (ps.length && ps[0].addEventListener) {

      for (var i=0, iLen=ps.length; i<iLen; i++) {
        p = ps[i];
        p.addEventListener ("DOMActivate", onActivate, false);
      }
      d.addEventListener("DOMActivate", onActivate, false);
    }
  }

  function onActivate () {
    console.log(this.id + ' has been activated');
  }

  function simulateActive(id) {
    var evt = document.createEvent("UIEvents");
    evt.initUIEvent("DOMActivate", true, false, window,1);

    var el = document.getElementById(id); 
    var cancelled = !el.dispatchEvent(evt);

    if(cancelled) {
      console.log("cancelled");

    } else {
      console.log("not cancelled");
    }
  }
</script>

 </head>

<body onload="Init ();">

  <div id="div0">div
    <p id="para0">para0</p>
    <p id="para1">para1</p>
    <button onclick="
      simulateActive('para0');
    ">Activate para</button>
  </div>
  <button onclick="
    simulateActive('para0');
  ">Activate para</button>

</body>
RobG
  • 142,382
  • 31
  • 172
  • 209
  • Thanks RobG. Unfortunately, the mousedown event doesn't trigger the :active styles in CSS (only tested in WebKit). It seems like there isn't a simple way to do this. :-/ – Evan Sharp Nov 17 '11 at 20:00
  • There is a [UIEvent interface](http://www.w3.org/TR/DOM-Level-2-Events/events.html#Events-UIEvent) that has a *DOMActivate* type, however it doesn't seem to trigger the associated UI behaviour. So the simple answer is it seems to be impossible with script. – RobG Nov 18 '11 at 04:44