1

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

4
  • Have you tried with out the brackets entirely? The expires object is expecting a value that it can parse as a date rather than an array. Commented Aug 7, 2013 at 14:31
  • the value assign to the cExpiry variable is not an array, is just a number ie. 5, when I call it using alert(cExpiry); it shows 5 which means the cookie expires in 5 days Commented Aug 7, 2013 at 14:33
  • how can I change the data type from string to number? Commented Aug 7, 2013 at 14:35
  • Placing squared brackets around it is making it an array. Commented Aug 7, 2013 at 14:35

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

6 Comments

When I call alert(cExpiry); it shows 5, the issue lies in the use of brackets or parenthesis or what ever I think is not properly being wrapped.
It might be a string, try using parseInt(cExpiry, 10) to cast it as a number
Indeed it is a string, how do I set the data type to number before submitting it in the form? furthermore what is the number 10 for?
The second parameter is called the "radix", basically which base to use. It's not required in modern browers, which always assume base 10, but in older browsers you can get unexpected results if you don't specify a radix. For example, numbers with a leading 0 could be parsed as octal (base 8). developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
Using parseint works, however Is there a way to set the data type before submitting it to the database? I am using <h3 style="margin-bottom: 0px;">Cookie Expiry</h3> <input style="width:100%" type="text" name="lu_ban_data[cExpiry]" value="<?php $cExpiry = get_option('lu_ban_data'); echo $cExpiry['cExpiry']; ?>" /></input>
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.