title: Save a dark mode checkbox toggle to local storage
body: <p>On my website I have a checkbox, which toggles between a default light theme and a dark one. If the page is set to dark mode and refreshed, the theme is switched back to light mode. Is there a way to save the visitors decision, so the page stays in dark mode on refresh? This is the code I currently have:</p>
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>const chk = document.getElementById('chk');
  
  chk.addEventListener('change', () =&gt; {
      document.body.classList.toggle('dark');
  });</code></pre>
  <pre class="snippet-code-css lang-css prettyprint-override"><code>body {
    background: #f5f5f5;
    color: #353535;
  }
  
  body.dark {
    background-color: #1a1a1a;
    color: #f5f5f5;
  }</code></pre>
  <pre class="snippet-code-html lang-html prettyprint-override"><code>        &lt;div class="toggle"&gt;
              &lt;input type="checkbox" class="checkbox" id="chk" /&gt;
              &lt;label class="label" for="chk"&gt;
          &lt;/div&gt;
          &lt;h1&gt;website text&lt;/h1&gt;</code></pre>
  </div>
  </div>
  </p>
  <p>I realise it might be a simple answer, but i am new to web development, and am struggling to find answers myself. Thank in advance you for all your help.</p>