16

Example, I have this cookie:

$.cookie("foo", "500", { path: '/', expires: 365 }); 

How do I get the value of that cookie and put it into a variable?

For example (I know this is not correct):

var foo = $.cookie("foo").val(); 

4 Answers 4

23

It's just var foo = $.cookie("foo").

There's no need for a .val() call as you're not accessing the value of a DOM element.

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

3 Comments

Damn, I had that before turns out I was using the wrong cookie!
ball , out of context question , is this plugin support dictionary also.
@kobe I do not understand what you are asking. Consider asking a separate question, or just experiment with the plugin yourself.
5

This worked for me:

function getCookieValue(cname) { // cname is the cookie name (foo) which value you are after var name = cname + "="; var decodedCookie = decodeURIComponent(document.cookie); var ca = decodedCookie.split(';'); for(var i = 0; i < ca.length; i++) { var c = ca[i]; while (c.charAt(0) == ' ') { c = c.substring(1); } if (c.indexOf(name) == 0) { return c.substring(name.length, c.length); } } return ""; } 

Comments

2

To get the value of a cookie, you can just call it's reference. For example:

$.cookie("foo", "somevalue"); alert($.cookie("foo")); 

Will alert:

somevalue 

1 Comment

How would you alert the 'expires' property?
2

By this way we can access

console.log($.cookie()); //It will gives the all cookies in the form of object

alert($.cookie('foo'));//it will give cookie foo value ie 500

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.