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

163
Views
How can I make "input type="checkbox" retain data value?

How can I rewrite this code:

var toggle = document.getElementById("test_switch");

var storedTheme = localStorage.getItem('theme') || (window.matchMedia("(prefers-color-scheme: dark)").matches ? "dark" : "light");
if (storedTheme)
    document.documentElement.setAttribute('data-theme', storedTheme)


toggle.onclick = function() {
    var currentTheme = document.documentElement.getAttribute("data-theme");
    var targetTheme = "light";

    if (currentTheme === "light") {
        targetTheme = "dark";
    }

    document.documentElement.setAttribute('data-theme', targetTheme)
    localStorage.setItem('theme', targetTheme);
};

so this retain its checkmarks:

<label for="mode_test">
    <input type="checkbox" id="test_switch">
</label>

I stumbled upon the code above in a blog post that included local storage and (prefers-color-scheme: dark) in their implementation of dark mode, I just don't know how to reconcile that with using checkboxes.

Thanks.

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

0

You can access checkbox value with .checked

var toggle = document.getElementById("test_switch");
console.log(toggle.checked) //returns true||false

With this you can easily check what is currently checked.

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!