Guys I am trying to catch something form the screen (email) when the form is opened & then save it in the cookie so I can use it for after the form is closed & show it somewhere else! I wrote this code. I hope its clean enough to be understood! Everything is working except that it is not saving in the cookie 🍪 here is the code:
//singelton works always
let start = isFormOppened();
while (start) {
if (isFormOppened && doesEmailInputExist && getBtn) {
console.log('code works');
getBtn().addEventListener('click', (e) => {
setCookie('email', getEmailValue(), 1);
console.log('cookie saved successfully');
start = false;
});
}
}
function isFormOppened() {
if (!document.querySelector('.cb-hp__wrapper')) {
return false;
}
return true;
}
function doesEmailInputExist() {
if (!document.querySelector('#email', '.cb-text__control')) {
return false;
}
return true;
}
function getBtn() {
if (document.querySelector('button', '.cb-button')) {
return document.querySelector('button', '.cb-button');
}
}
function getEmailValue() {
if (!document.querySelector('#email', '.cb-text__control')) {
return -1;
}
return document.querySelector('#email', '.cb-text__control').value;
}
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 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;
}
function eraseCookie(name) {
document.cookie =
name + '=; Path=/; Expires=Thu, 01 Jan 1970 00:00:01 GMT;';
}