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?
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.
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.
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.
To clarify, you CAN NOT give :hover to a pseudo element.
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();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With