-4

I wrote this HTML code

 <select value="bgColor" id="selectColor" onchange="changeColor();">
      <option value="selectTheColor">Select color</option>
      <option value="black">Black</option>
      <option value="blue">Blue</option>
      <option value="red">Red</option>
  </select>

and this is my JS code, everything works fine except cookie, how do I solve this please?

 function changeColor(){
          var selectedColor = document.getElementById("selectColor").value;
          if(selectedColor != "selectTheColor"){
            document.bgColor = selectedColor;
            document.cookie = "color="+selectedColor+";expires=Mon, 5 Mar 2018 02:00:00 UTC";
          }
        }

   window.onload = function(){
        if(document.cookie.length != 0){
          var nameValueArray = document.cookie.split("=");
          document.getElementById("selectColor").value = nameValueArray[1];
          document.bgColor=nameValueArray[1];
        }
      }

1 Answers1

0

You've set the cookie to expire at 2am UTC. It's currently 6:50pm UTC.

ceejayoz
  • 176,543
  • 40
  • 303
  • 368