/    Sign up×
Community /Pin to ProfileBookmark

Save/delete cookies on check/uncheck

Hello,
My knowledge on JS is very poor.
I have a form with several checkboxes
I want to know if it is possible to set a cookie with the checkbox id when the user checks it and delete if he unchecks it, and also how to do that.
Thanks in advance.

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@slaughtersOct 20.2009 — Here are some functions I use to set and get Cookie values:[CODE]//////////////////////////////////////////////////////////////////////////////
// setCookie
//
// Create a client side cookie.

//
// @param c_name - Name to call the cookie
// @param value - Value to assign to the cookie
// @param expiredays - *optional* number of days before cookie expires

//////////////////////////////////////////////////////////////////////////////
function setCookie(c_name, value, expiredays) {
var exdate = new Date();
exdate.setDate(exdate.getDate() + expiredays);
document.cookie = c_name + "=" + escape(value) + ((expiredays == null) ? "" : ";expires=" + exdate.toGMTString());
}

//////////////////////////////////////////////////////////////////////////////
// getCookie
//
// Get value of a client side cookie
//
// @param c_name - Cookie name.

// @return cookie value, or an empty string if cookie is not found
//////////////////////////////////////////////////////////////////////////////
function getCookie(c_name) {

if (document.cookie.length > 0) {
c_start = document.cookie.indexOf(c_name + "=");
if (c_start != -1) {
c_start = c_start + c_name.length + 1;
c_end = document.cookie.indexOf(";", c_start);
if (c_end == -1) c_end = document.cookie.length;
return unescape(document.cookie.substring(c_start, c_end));
}
}

return "";
}[/CODE]
You would call the setCookie function with the "onclick" condition in the INPUT tag of your checkbox
Copy linkTweet thisAlerts:
@marcianoauthorOct 20.2009 — Hello!

I have something like this function

'id' is the checkbox id.

For deletion, wouldn't be enough to set the same cookie expiration time to a date before than now?

function SDCookie(id, name, value, path, domain) {

if (id.checked == 1) {

var expires = -365 * 1000 * 60 * 60 * 24;

}

else {

var expires = 365 * 1000 * 60 * 60 * 24;

}

var today = new Date();

today.setTime( today.getTime() );

var expires_date = new Date( today.getTime() + (expires) );

document.cookie = name + "=" +escape( value ) +
( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) +
( ( path ) ? ";path=" + path : "" ) +
( ( domain ) ? ";domain=" + domain : "" );

}


Check or uncheck the checkbox, the cookie is updated like check case.

My problem is here: if (id.checked == true)
×

Success!

Help @marciano spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...