简体   繁体   中英

How to set cookie to value of checkbox

I am trying to make a function that sets a cookie to a true/false value depending on if a checkbox has been clicked or not. I can then use this value when the page loads to render the checkbox as toggled.

HTML:

<form name="example">
  <div class="checklist-item">
    <label for="example">Example</label>
    <input type="checkbox" id="example" name="example">
  </div>
</form>

JS:

document.getElementById("example").addEventListener("click", exampleCookie)
function exampleCookie() {
    document.cookie = "exampleCookie";
}

How might I use the function exampleCookie to be equal to the value of the checkbox?

sorry, i give you a referrence from here https://www.javatpoint.com/javascript-cookies maybe it helps

 <!DOCTYPE html> <html> <head> </head> <body> <select id="color" onchange="display()"> <option value="Select Color">Select Color</option> <option value="yellow">Yellow</option> <option value="green">Green</option> <option value="red">Red</option> </select> <script type="text/javascript"> function display() { var value = document.getElementById("color").value; if (value != "Select Color") { document.bgColor = value; document.cookie = "color=" + value; } } window.onload = function () { if (document.cookie.length != 0) { var array = document.cookie.split("="); document.getElementById("color").value = array[1]; document.bgColor = array[1]; } } </script> </body> </html>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM