My jquery cookies are not resetting, even though am using the correct code.
Posted
by Adam Libonatti-Roche
on Stack Overflow
See other posts from Stack Overflow
or by Adam Libonatti-Roche
Published on 2010-01-12T11:43:55Z
Indexed on
2010/04/21
5:03 UTC
Read the original article
Hit count: 367
My problem is that I am trying to reset some form cookies so when someone has completed their form, they are reset so it is possible for someone else to complete the form. Simple and obvious.
But
However many different lines of code I put in, the cookies just do not seem to be disappearing. I am using the remember function from the site below:
So the details stay when they move away from the page: the code i have for the page starting is as follows:
<script type="text/javascript">
function remember( selector ){
$(selector).each(
function(){
//if this item has been cookied, restore it
var name = $(this).attr('name');
if( $.cookie( name ) ){
if( $(this).is(':checkbox') ){
$(this).attr('checked',$.cookie( name ));
}else{
$(this).val( $.cookie(name) );
}
}
//assign a change function to the item to cookie it
$(this).change(
function(){
if( $(this).is(':checkbox') ){
$.cookie(name, $(this).attr('checked'), { path: '/', expires: 1 });
}else{
$.cookie(''+name+'', $(this).val(), { path: '/', expires: 1 });
}
});
});
}
// JQUERY FOR THIS PAGE
$(document).ready( function(){
remember("[name=username]");
remember("[name=firstname]");
remember("[name=lastname]");
remember("[name=email]");
remember("[name=password]");
remember("[name=address1]");
remember("[name=address2]");
remember("[name=postcode]");
remember("[name=country]");
} );
</script>
And the code for resetting them is simple enough, as it takes the cookie name and sets it to null. However, this does not work as on returning to the form, all fields from before are still there.
Any help with this would be brilliant.
© Stack Overflow or respective owner