Remember me check box cookie for login form
So i have tried to setup a “Remember me” checkbox and a javascript cookie, but can’t get it to work. It’s as the cookie doesn’t get set, hence the “Remember me” function doesn’t work.
Here is the javascript code:
<script>
document.addEventListener('DOMContentLoaded', function() {
// Check if the "Remember Me" checkbox is present
var rememberMeCheckbox = document.getElementById('remember_me');
if (rememberMeCheckbox) {
// Check if the cookie is set and update the checkbox accordingly
if (getCookie('rememberMe') === 'true') {
rememberMeCheckbox.checked = true;
}
// Add an event listener to the checkbox
rememberMeCheckbox.addEventListener('change', function() {
// Set or delete the cookie based on the checkbox state
setCookie('rememberMe', this.checked, 30); // 30 is the cookie expiration in days
});
}
// Function to set a cookie
function setCookie(name, value, days) {
var expires = '';
if (days) {
var date = new Date();
date.setTime(date.getTime() (days 24 60 60 1000));
expires = '; expires=' date.toUTCString();
}
document.cookie = name '=' value expires '; path=/';
}
// Function to get a cookie value
function getCookie(name) {
var nameEQ = name '=';
var ca = document.cookie.split(';');
for (var i = 0; i < ca.length; i ) {
var c = ca[i];
while (c.charAt(0) === ' ') c = c.substring(1, c.length);
if (c.indexOf(nameEQ) === 0) return c.substring(nameEQ.length, c.length);
}
return null;
}
});
</script>
Upvoters
Feature Details
Status:
Board: Divi Form Builder
Created: 18 July 2024
Last Updated: 18 July 2024
Posted By: bo.lange
Join the conversion, Login to Reply