Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

144
Views
Is there any way to change background color of ::selection (selected text) using JavaScript?

I want to change the background of my text selection using HTML button. Is there any way to change background color of ::selection (selected text) using JavaScript?

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

In general where it's not possible to directly change pseudo element settings via JS it is possible to set CSS variables using JS.

So if you have something like this in your stylesheet:

::selection {
  background-color: var(--selcolor);
}

and something like this in your JS:

   element.style.setProperty('--selcolor', selcolor)

it will work.

Here's small example. It changes the selection color variable in the body:

function selChange(color) {
  document.body.style.setProperty('--selcolor', color);
}
body {
  --selcolor: yellow;
}

::selection {
  background-color: var(--selcolor);
}
<button onclick="selChange('cyan');">Cyan</button>
<button onclick="selChange('magenta');">Magenta</button>
<button onclick="selChange('yellow');">Yellow</button>
<p>Highlight some of this text, then click a color and highlight some text...</p>

about 4 years ago · Juan Pablo Isaza Report

0

  • you can make it by toggle class give your body a class which will take the default selection style and make a class to toggle it when click the button

Here's example in pure CSS

<!DOCTYPE html>
<html>
<head>
  <style>
    .body-selection-green *::selection {
      background: green;
    }

    .body-selection-yellow *::selection {
      background: yellow
    }
  </style>
</head>

<body class="body-selection-green">
  <div> Try to select me </div>
  <button> change selection background</button>
  <script>
    let btn = document.getElementsByTagName("button")[0];
    btn.addEventListener("click", function() {
      if(document.body.classList.contains("body-selection-green")) {
           document.body.classList.remove("body-selection-green")
           document.body.classList.add("body-selection-yellow")
       } else if(document.body.classList.contains("body-selection-yellow")) {
           document.body.classList.remove("body-selection-yellow")
           document.body.classList.add("body-selection-green")
       }
    });
  </script>
</body>
</html>
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!