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

487
Views
How to save new background color via `colorpicker` to `localStorage`?

I know there's a lot of similar questions.. I've tried them but I really can't incorporate it to my project.

I'm trying to save to localStorage the new background-color selected using the native colorpicker.

I'm almost there but I just can't figure out how to make it work.

Please see my code so far:

function changeBgColor(color) {
  if (color) window.localStorage.setItem('bgColor', color);
  else if (!(color = window.localStorage.getItem('bgColor'))) return;

  document.getElementById('colorpicker').addEventListener('input', function() {

    var elements = document.getElementsByClassName("card-bg")
    for (var i = 0; i < elements.length; i++) {
      elements[i].style.background = this.value;
    }

  })

}
window.addEventListener('DOMContentLoaded', () => changeBgColor());
.card1 {
  width: 200px;
  height: 200px;
  background-color: #222;
  margin: 10px 0 0 0;
}
<div class="card1 card-bg">Set A</div>
<div class="card1 card-bg">Set A</div>
<div class="card1 card-bg">Set A</div>

<br>

<input type="color" id="colorpicker" onselect="changeBgColor();">

The important feature for me are:

  1. To change background color using input="color"
  2. Use class selector since I have multiple divs I want to target with the same input
  3. Save the value to localStorage

That's all really. I just need to figure out the part where the value gets saved to localStorage.

After I make this work, I will need to replicate it for a different set of divs..

Thank you in advance for any help.

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

0

If check had = assignment instead of == comparison

Here is a working refactored snippet:

let pretend_local_storage = null

function setupBackgroundColorChange() {
  const elements = document.querySelectorAll(".card-bg")
  document.getElementById('colorpicker').addEventListener('input', function(e) {
    color = e.target.value
    if (color === pretend_local_storage) return
    pretend_local_storage = color
    elements.forEach((element) => element.style.background = color)
  })
}
window.onDOMContentLoaded = setupBackgroundColorChange();
.card1 {
  width: 200px;
  height: 200px;
  background-color: #222;
  margin: 10px 0 0 0;
}
<div class="card1 card-bg">Set A</div>
<div class="card1 card-bg">Set A</div>
<div class="card1 card-bg">Set A</div>

<br>

<input type="color" id="colorpicker" onselect="changeBgColor();">

Local Storage example:

localStorage.getItem('bgColor', null)
function setupBackgroundColorChange() {
    const elements = document.querySelectorAll(".card-bg")
    setColor(elements, localStorage.getItem('bgColor', '#000'))
    document.getElementById('colorpicker').addEventListener('input', function (e) {
        color = e.target.value
        if (color === localStorage.getItem('bgColor')) return
        localStorage.setItem('bgColor', color)
        setColor(elements, color)
    })
}
function setColor(elements, color) {
  elements.forEach((element) => element.style.background = color)
}
window.onDOMContentLoaded = setupBackgroundColorChange();
about 4 years ago · Juan Pablo Isaza Report

0

First of all i would use onchange trigger. To be honest, you dont need any condition inside the function. you can set the color to localStorage and that is it.

/* this lines check if already set color to localStorage */
  if (window.localStorage.getItem('bgColor')) {
  var elements = document.getElementsByClassName("card-bg")
  for (var i = 0; i < elements.length; i++) {
    elements[i].style.background = this.value;
  }
}

function changeBgColor() {
  let color = document.getElementById('colorpicker').value;
  const bg_curr = localStorage.getItem('bgColor');
  console.log(color, bg_curr);
  localStorage.setItem('bgColor', color);
  

  document.getElementById('colorpicker').addEventListener('input', function() {

    var elements = document.getElementsByClassName("card-bg")
    for (var i = 0; i < elements.length; i++) {
      elements[i].style.background = color;
    }

  })

}
window.addEventListener('DOMContentLoaded', () => changeBgColor());
.card1 {
  width: 200px;
  height: 200px;
  background-color: #222;
  margin: 10px 0 0 0;
}
<input type="color" id="colorpicker" onchange="changeBgColor();">
<div class="card1 card-bg">Set A</div>
<div class="card1 card-bg">Set A</div>
<div class="card1 card-bg">Set A</div>

<br>

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!