Activate an Element's :Active CSS Pseudo-Class Using JavaScript

How to pass css :active pseudo class to javascript?

You can use the document.activeElement for that.

$(function() {  $(document).on('click', function() {    console.log(document.activeElement);  });});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><div id="a">asdf</div><span>123</span><select>  <option>1</option></select><button>123</button><input />

Detect pseudo CSS activation

Pseudo elements are not part of the DOM so you cannot trigger events on them.

https://developer.mozilla.org/en-US/docs/Web/CSS/Pseudo-elements

In addition .style prototype function does not work as intended, to look for computed style use

var ele = document.querySelector('.example-value')
window.getComputedStyle(ele, null).backgroundColor === "rgba(0,0,0,0.75)"

https://developer.mozilla.org/en-US/docs/Web/API/Window/getComputedStyle

How to trigger the :active pseudoclass on keyboard 'enter' press? (using only CSS)

Good question!

Yeah, you can't do this anymore.

A long time ago MSIE treated :active like :focus so there was sort of a way to accomplish this with hyperlinks (this was before gigabit internet speeds and when browsers didn't do a good job of showing load-in-progress except for a dumb waving flag or something).

Run the below Snippet to see the behavior in action:

  • input type='button' can be activated with ENTER or SPACE
  • Holding down SPACE on a button will show the :active style (except for FireFox; this looks like FF bug since it works OK for mousedown)
  • Holding down ENTER on a button will repeatedly trigger onclick every time your keyboard sends the character.
  • Holding down SPACE on a button will trigger onclick only if the SPACE key is released while still focused on the button. (For example, you can simulate mouse click this way: tab to a button, hold down space, then hit tab again and release it to cancel the click.)
  • Hyperlinks can be activated with ENTER only.
  • Clicking on hyperlinks will show :active style, using ENTER (or touch) will not.

document.getElementById('t1').focus(); // set focus for easier demo
:active{   color: Red;}
<input type='text' id='t1' value='Set focus and hit tab'/><a href='javascript:;' onclick='console.log("Hyperlink")'>Hyperlink</a><input type='button' value='Button' onclick='console.log("Button")'/>

Selecting and manipulating CSS pseudo-elements such as ::before and ::after using javascript (or jQuery)

You could also pass the content to the pseudo element with a data attribute and then use jQuery to manipulate that:

In HTML:

<span>foo</span>

In jQuery:

$('span').hover(function(){
$(this).attr('data-content','bar');
});

In CSS:

span:after {
content: attr(data-content) ' any other text you may want';
}

If you want to prevent the 'other text' from showing up, you could combine this with seucolega's solution like this:

In HTML:

<span>foo</span>

In jQuery:

$('span').hover(function(){
$(this).addClass('change').attr('data-content','bar');
});

In CSS:

span.change:after {
content: attr(data-content) ' any other text you may want';
}

Selecting Pseudo Element :focus and :active via JS

I actually found a great solution for this. I've read some tutorials for selecting pseudo elements :after and :before on here but had never seen something that worked properly for :focus and :active.

My problem: I wanted to have different CSS for certain elements on the page based on the time of day but without switching out the entire stylesheet. (It's a simple site and that felt sloppy to me). I used: $('.text_box').css('text-shadow', '0 0 0 #272727'); to adjust the CSS elements but ran into a snag when I was trying to adjust the styled pseudo elements.

To make it word I used the addRule method, which allowed me to adjust the CSS in the the time-sensitive CSS JS script that I had put together.

(function($) {
window.addRule = function(selector, styles, sheet) {
if (typeof styles !== "string") {
var clone = "";
for (var style in styles) {
var val = styles[style];
style = style.replace(/([A-Z])/g, "-$1").toLowerCase();
clone += style + ":" + (style === "content" ? '"' + val + '"' : val) + "; ";
}
styles = clone;
}
sheet = sheet || document.styleSheets[0];
if (sheet.insertRule) sheet.insertRule(selector + " {" + styles + "}", sheet.cssRules
.length);
else if (sheet.addRule) sheet.addRule(selector, styles);
return this;
};
if ($) {
$.fn.addRule = function(styles, sheet) {
addRule(this.selector, styles, sheet);
return this;
};
}
}(window.jQuery));
$(document).ready(function() {
function night() {
var currentTime = new Date().getHours();
if (0 <= currentTime && currentTime < 5) {
$('.text_box').css('text-shadow', '0 0 0 #272727');
$(".text_box:focus").addRule("background-color: #272727");
$(".text_box:focus").addRule("color: #FFF");
$(".text_box:focus").addRule("-moz-box-shadow: 0 0 1em #272727");
$(".text_box:focus").addRule("-webkit-box-shadow: 0 0 1em #272727;");
$("#mc-embedded-subscribe").addRule("color: #272727;");
$(".submit_box:hover").addRule("background-color:#272727 !important;");
$(".submit_box:hover").addRule("color:#FFF !important;");
$(".submit_box:hover").addRule("-moz-box-shadow: 0 0 1em #272727");
$(".submit_box:hover").addRule("-webkit-box-shadow: 0 0 1em #272727;");
}
if (20 <= currentTime && currentTime <= 24) {
$('.text_box').css('text-shadow', '0 0 0 #272727');
$(".text_box:focus").addRule("background-color: #272727");
$(".text_box:focus").addRule("color: #FFF");
$(".text_box:focus").addRule("-moz-box-shadow: 0 0 1em #272727");
$(".text_box:focus").addRule("-webkit-box-shadow: 0 0 1em #272727;");
$("#mc-embedded-subscribe").addRule("color: #272727;");
$(".submit_box:hover").addRule("background-color:#272727 !important;");
$(".submit_box:hover").addRule("color:#FFF !important;");
$(".submit_box:hover").addRule("-moz-box-shadow: 0 0 1em #272727");
$(".submit_box:hover").addRule("-webkit-box-shadow: 0 0 1em #272727;");
}
}
night();
});

This worked great, though I am wondering if there is an even easier workaround? This was a lot simpler+cleaner then adding new classes to the existing elements that contain :active or :focus -- though I don't think that would work properly in this case.



Related Topics



Leave a reply



Submit