Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Removing the :active pseudo-class from an element

I'd like to be able to tell an element that it is no longer :active so that the CSS rules no longer apply. Is there any way to do this in JavaScript?

like image 700
basicallydan Avatar asked Feb 25 '14 14:02

basicallydan


People also ask

What is active pseudo class?

The :active CSS pseudo-class represents an element (such as a button) that is being activated by the user. When using a mouse, "activation" typically starts when the user presses down the primary mouse button.

How do you change pseudo element?

In general, if we want to change anything in pseudo elements through JavaScript, we do it in the following way: Create CSS classes on element, which will change pseudo elements' UI. Get the element using querySelector. Modify the classes using classList.

How do I use active pseudo class?

Pseudo class is to show different state of an element or a css selector. Active pseudo class is to show that the element is in active state. This pseudo class is mostly being associated with link and button or any other element which can be active. For example if it is associated with link that the link is active.

Can you hover a pseudo element?

To clarify, you CAN NOT give :hover to a pseudo element.


1 Answers

Possible solutions :

1) Using classes :

JS :

document.getElementById("element").classList.remove("hasactive");

CSS :

#element.hasactive:active {
    background:blue;
}

2) Preventing default mousedown functionality (active state) :

EDIT : Apparently, this only works on Firefox.

JS :

document.getElementById("element").onmousedown = function(event) {
    event.preventDefault();
}

3) Removing a style element with the css rule in it

HTML :

<style id="activestyle">
#element:active {
/*Your code here*/
}
</style>

JS :

document.getElementById("activestyle").remove();
like image 65
Tim Nguyen Avatar answered Sep 28 '22 04:09

Tim Nguyen