Reputation: 2142
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?
Upvotes: 17
Views: 27616
Reputation: 179
For jQuery users:
JS:
$(".btn").click( function () { $(this).blur(); } );
HTML:
<button type="button" class="btn btn-success">press me</button>
Upvotes: 1
Reputation: 497
Here is a trick by resetting the DOM element.
function resetElement(e){
var $e = $(e);
var $original = $e.clone();
$e.replaceWith($original);
}
Then call it with:
resetElement(document.activeElement);
Upvotes: 2
Reputation: 1213
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();
Upvotes: 12
Reputation: 11245
document.activeElement = null;
OR
document.activeElement && document.activeElement.blur();
Upvotes: 0