4

I am using a jQuery cookie plugin called JS Cookie and what happens is that I am unable to retrieve the data or the value in the cookie. I would like to store the value of the cookie that was inputted on the text box and retrieve it in the text field when the page is visited again.

$(document).ready(function() { var cookie_email = Cookies.get('user_email'); $('#email_address').val(cookie_email); $('#test_button').click(function() { Cookies.set('user_email', email_address, { expires: 365 }); }); }); 

https://jsfiddle.net/mue1amcm/

1
  • Are you testing only on JSFiddle? Or locally too? Commented Mar 17, 2016 at 11:12

1 Answer 1

5

Your code doesn't work as you don't assign the email_address variable anywhere. You should change that value to use $('#email_address').val().

Also note that your jsFiddle isn't setup correctly; your URL to the external cookie script goes to a Github page. You need to use a CDN link instead. Try this:

var cookie_email = Cookies.get('user_email'); $('#email_address').val(cookie_email); $('#test_button').click(function() { Cookies.set('user_email', $('#email_address').val(), { expires: 365 }); }); 

Working example

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

4 Comments

I'm trying to test here, but even when I pass the parameters hardcoded, when I try to make a get, there is nothing on cookies.
Are you testing on a server? You cannot set a cookie when testing on the local file system. As you can see from the fiddle, it should be working fine
just a question, when I do this var myemail = $('#email_address').val(); Cookies.set('user_email', myemail , it doesnt seem to work, isnt that suppose to be equal
Assuming that the myemail variable is set at the right time to capture user input it should work. If you're still having issues I would suggest you open a new question as it has now progressed to a logic flow problem. Remember to include the full code sample in the question, as well as exactly what you want to happen.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.