David Garcia
David Garcia

Reputation: 2696

jQuery: using localized variable

I am trying to use jQuery Cookie in order to show/hide a div element.

var cExpiry = lu_ban_object.cExpiry;

jQuery('.float_close').click(function () {
                jQuery('.float_notice').fadeToggle('slow');
                jQuery.cookie('noticeVisibility', 'hidden', {
                    expires: [cExpiry], //problem is here
                    path: '/'
                });

The expires: would be a number and it represents the cookie expiry day. that number is being stored in an array and then localized, I have assigned that localized numebr to the cExpiry variable, however it is not accepting the brackets, [] I have tried () and {} but it is not working, also +[cExpiry]+

I get the following error;

Uncaught TypeError: Object [object Array] has no method 'toUTCString' 

How do I change the data type to number? according to the screenshot it is saved as string.

jquery

Upvotes: 1

Views: 261

Answers (1)

colestrode
colestrode

Reputation: 10658

expires needs to be a Date object or a number. From your question, it looks like cExpiry is a number already, so no need to cast it as an Object or Array by wrapping it in brackets.

cExpiry might stored as a string, if that's the case then you can use parseInt to cast it as a number: parseInt(cExpiry, 10);

From the documentation:

Value can be a Number which will be interpreted as days from time of creation or a Date object

Upvotes: 1

Related Questions