Splited string of cookie is not working

Mostafa Khan

Considering this is the HTML code,

function changeColor(){
       var selectedColor = document.getElementById("selectColor").value;
       if(selectedColor != "selectTheColor"){
          document.bgColor = selectedColor;
          document.cookie = "color="+selectedColor+";expires=Fri, 2 Mar 2018 02:00:00 UTC";
          var splited = document.cookie.split("="); // codes here not working
          alert(splited[1]);
       }
    }
<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>

why is the code not working on the split?

messerbill

Assigning a string over more that one line is not supported this way (using '', you have to use ` ` instead). Do the following instead:

function changeColor(){
   var selectedColor = document.getElementById("selectColor").value;
   if(selectedColor != "selectTheColor"){
      document.bgColor = selectedColor;
      var test = "color="+selectedColor+`;expires=Fri, 2 Mar 2018 
      02:00:00 UTC`;
      var splited = test.split("="); // codes here not working
      alert(splited[1]);
   }
}
<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>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related