Alexander
Alexander

Reputation: 4219

jQuery Cookie and Variables

I'm currently using jQuery cookie for a button

$('.vote').one('click', function(){      
    var cookieid = ($(this).data("id")); //cookie name
    alert ($.cookie(cookieid));  //..etc

This gives me a textbox saying "undefined". However, I do know that the variable cookieid is assigned, as alerting the variable gives me a value.

Furthermore, this script works fine if I use a 'cookieid' that I know I have assigned. For instance, I have a cookie named 17. When I alert ($.cookie('17')); everything works correctly.

I'm pretty sure the problem is not in my end of the code, so can jQuery cookie not handle variables?

Edit:

//creating cookie if none already exists
var cookieid = ($(this).data("id")); //cookie name
        if ($.cookie(cookieid) == 'active') //if cookie exists
        {
            //do nothing
        }
        else
        {
            $.cookie(cookieid, 'active');
        }

Upvotes: 0

Views: 52

Answers (1)

user2616876
user2616876

Reputation:

($(this).data("id")) returns a number.

alert(typeof $(this).data("id")); //number

Try converting it to a string:

alert ($.cookie(cookieid.toString())); 

Here's a working example: jsfiddle

Upvotes: 1

Related Questions