title: Save Button-Click Count in localStorage in javascript
body: <p>I made a button click counter for a website using some JavaScript.
  The counter works well, but now I'm stuck in making the saving of the count. You know, if I click the button 3 times, the text says 3 Times. But I want to save that value so if the user refreshes the page, it should display 3 Times again.</p>
  
  <p>I knew of using <strong>localStorage</strong>, I followed a simple tutorial and applied it to my code, but it does not seem to be working. When I run the page in Microsoft Edge and see the Debug page (F12), the console throws an error that says: <code>Unable to get property 'getItem' of undefined or null reference</code>. I searched in other posts but no one of these could solve my problem. It seems to be stuck when retrieving the value in localStorage.</p>
  
  <p>This is my code:</p>
  
  <pre><code>&lt;!DOCTYPE HTML&gt;
  &lt;html&gt;
  &lt;head&gt;
  &lt;meta http-equiv="Content-Type" content="text/html; charset=utf-8"&gt;
  &lt;title&gt;Increment count when button is clicked&lt;/title&gt;
  &lt;/head&gt;
  
  &lt;body&gt;
  &lt;input type="button" value="Registrar" id="countButton" /&gt;
  &lt;input id="ocityField" type="text" value="" placeholder="Ciudad de Origen"/&gt;
  &lt;input id="cityField" type="text" value="" placeholder="Ciudad de participación"/&gt;
  &lt;input id="name" type="text" value="" placeholder="Nombre"/&gt;
  &lt;p&gt;Personas Registradas: &lt;span id="displayCount"&gt;0&lt;/span&gt;&lt;/p&gt;
  
  &lt;script type="text/javascript"&gt;
  var count = 0;
  var button = document.getElementById("countButton");
  var display = document.getElementById("displayCount");
  var textbox = document.getElementById("ocityField");
  var textboxa = document.getElementById("cityField");
  var textboxb = document.getElementById("name");
  if(window.localStorage.getItem('count')){
      var savedcount = window.localStorage.getItem('count');
      count = window.localStorage.getItem('count');
  }else{
      count = 0;
  }
  display.innerHTML = count;
  button.onclick = function(){
      var mystring = textbox.value;
      var mystring2 = textboxa.value;
      var mystring3 = textboxb.value;
      if(!mystring.match(/\S/) || !mystring2.match(/\S/) || !mystring3.match(/\S/)) {
          alert ('Empty value is not allowed');
          return false;
      } else {    
          count++;
          window.localStorage.setItem('count', count);
          display.innerHTML = count;      
          textbox.value = "";
          textboxa.value = "";
          textboxb.value = "";
          return true;
          }
  }
  &lt;/script&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>I tried using <code>window.localStorage</code> and just <code>localStorage</code> but no one did work.</p>