-1
<script type="text/javascript">
    function disableButton(btn){
    document.getElementById(btn.id).disabled = true;
        alert("Button has been disabled.");
    }
</script>

<button id="btn1" onclick="disableButton(this)">$200</button>

I'm trying to remember if a button has been clicked and still be disabled, even if the user reloads the page. I'm fine using JS and jQuery, but I'd like to not use PHP. Is there a way I could use cookies and local storage to remember it?

Lauren Yim
  • 12,700
  • 2
  • 32
  • 59

1 Answers1

0

You can use Local Storage to achieve this.

<script type="text/javascript">
  window.onload = function () {
    var isButtonDisabled = localStorage.getItem('buttonDisabled') === 'true';
    if (isButtonDisabled) {
      document.getElementById('btn1').disabled = true;
    }
  };

  function disableButton(btn) {
    document.getElementById(btn.id).disabled = true;
    alert('Button has been disabled.');
    localStorage.setItem('buttonDisabled', 'true');
  }
</script>
Community
  • 1
  • 1
Nathan
  • 268
  • 2
  • 7