Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript : Change selection background

How should I do this in Javascript?

// CSS
*::-moz-selection{
 background:transparent; 
}

I tried these but doesn't really works:

// Javascript
document.mozSelection = "transparent";
document.body.mozSelection = "transparent";
like image 493
Adam Halasz Avatar asked Mar 09 '26 19:03

Adam Halasz


1 Answers

You can add a rule to the stylesheet.

// Get the first stylesheet 
var ssheet = document.styleSheets[0];

if ("insertRule" in ss) {
    ss.insertRule('#yourdivcontainer::-moz-selection { background: transparent; }', 0);        
}

IMO, it's not a good practice anyway. You should create a CSS class with the selection color and change the class itself via JS instead of the style.

like image 57
Soufiane Hassou Avatar answered Mar 12 '26 10:03

Soufiane Hassou



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!