JSFiddle - React, Tailwind, and code Playground
HTML
<button onClick="setCookie('law_cookie', 'agree_all', 90)">Save cookie</button><br/>
<button onClick="showCookies()">Show cookies</button><br/>
<textarea id=cookies></textarea>
JavaScript
function setCookie(name, value, daysToLive) {
// Encode value in order to escape semicolons, commas, and whitespace
let cookie = name + "=" + encodeURIComponent(value);
if (typeof daysToLive === "number") {
/* Sets the max-age attribute so that the cookie expires
after the specified number of days */
cookie += ";max-age=" + (daysToLive * 24 * 60 * 60) + ';path=/';
window.alert(cookie);
document.cookie = cookie;
cookie_set = true
}
}
function showCookies() {
const output = document.getElementById('cookies')
output.textContent = '> ' + document.cookie
}