[Solved] How to save background color in cookie through button click?
You can do it like this: window.addEventListener(‘DOMContentLoaded’, function() { var cookieValue = getCookie(‘backgroundColor’), btns = document.querySelectorAll(‘.color-btn’); if (cookieValue) { setBackgroundColor(cookieValue); } Array.from(btns).forEach(function(btn) { btn.addEventListener(‘click’, function() { var color = this.getAttribute(‘data-color’); setBackgroundColor(color); }); }); }); function setBackgroundColor(color) { document.body.style.backgroundColor = color; setCookie(‘backgroundColor’, color); } function getCookie(name) { var cookies = document.cookie.split(‘;’), cookie = cookies.find(function(str) { return … Read more