передать код цвета в локал стородж из переменной

Нужно вставить значение из переменной цвета в динамическую переменную, чтобы передать цвет из <input type="color"> и отобразить её значение value в localStorage. У меня почему то не получилось, поэтому хочется понять почему. Большое спасибо за любую помощь! )

window.onload = function() {
  if (localStorage.getItem('bgcolor') !== null) {
    let color = localStorage.getItem('bgcolor');
    document.getElementsByTagName('body')[0].style.background = color;
  }
  document.getElementById('dark').onclick = function() {
    document.getElementsByTagName('body')[0].style.background = 'black';
    localStorage.setItem('bgcolor', 'dark');
  }
  document.getElementById('light').onclick = function() {
    document.getElementsByTagName('body')[0].style.background = 'white';
    localStorage.setItem('bgcolor', 'light');
  }
  let gdd;
  gdd = document.getElementById("bgColours").value; //поместь в локал стородж код цвета, а не значение переменной
  document.getElementById('custom-choise').onclick = function() {
    document.getElementsByTagName('body')[0].style.background = document.getElementById("bgColours").value;
    localStorage.setItem('bgcolor', `${gdd}`); // хочется сюда записать значение из переменной, которое бы отображалось в локлстордж
    otobr.innerHtml = document.getElementById("bgColours").value;
  }
}
document.querySelector('#bgColours').addEventListener('change', function(event) {
  console.log('chto-to-tyt-bydet');
  document.getElementsByTagName('body')[0].style.background = 'event.target.value';
});
<button id='dark'>Dark</button>
<button id='light'>Light</button>
<button id='custom-choise'>custom choise</button>
<input type='color' id='bgColours'>
<div id='otobr'></div>


Ответы (1 шт):

Автор решения: CbIPoK2513

У вас не получилось, потому что переменная gdd (которая хранит в себе цвет), НЕ находится внутри слушателя, где мы по логике должны получать цвет input и передавать его в localStorage.

document.getElementById('custom-choise').onclick = function() {
  let gdd = document.getElementById("bgColours").value;
  // Дальше ваш код
→ Ссылка