Using the jQuery Cookies plugin, I am setting a cookie, the value of which is a number.
$.cookie('xyz_id','1');
I need to use this number to get the index of an element in my HTML.
var $curr = $.cookie('xyz_id');
var $el = $('#main li:eq($curr)');
The output of $curr is 1. So why does the typeOf() $curr show as a string instead of a number? My attempts to convert it to a number using parseInt() have failed. Not sure what’s going on….
Quotes make a variable a string:
However, in this case, cookies are effectively strings:
http://www.quirksmode.org/js/cookies.html
Instead, do:
Then, when reading the value back, cast it back to a numeric value using
parseInt().Which should create a numeric value.
Also,
parseIntshould work (within reason) for typecasting a number from a string. See:http://jsfiddle.net/CqBw6/