0

im kind of new to JQuery... I would like to save the value of a form field as a cookie once i click on the button but it does not seem to work. The code i'm using is the following

$(function() {
  $("#myButtonId").click( function() {
     var myVar = $('#myTextField').val();
     $.cookie("cookie", myVar);

     //i try the next line just to control if the cookie was saved
     alert($.cookie("cookie"));
   });
});

When the alert pops up it just shows "undefined" as an output (meaning the cookie was not set), if i set the cookie outside the click function it works fine.

EDIT: if i may be of help... the button is the submit button to login to my web application, i want to save the login data so next time i open the application i dont have to rewrite the login data

Felix
  • 140
  • 10
  • Can you include jquery library? – Sudharsan S Aug 27 '14 at 12:41
  • 1
    What value you get for `alert($('#myTextField').val());` ? – Murali Murugesan Aug 27 '14 at 12:42
  • have you included the [cookie library](http://plugins.jquery.com/cookie/) or are there any errors in your console? Or is your button in a form - if so you may need to prevent it's default action – Pete Aug 27 '14 at 13:11
  • let me answer... 1) im using the https://github.com/carhartl/jquery-cookie library 2) i dont have any errors on the console 3) what do you mean by: prevent the button default action? (yes, it makes part of a form) 4) The value i get is "undefined" – Felix Aug 27 '14 at 13:21
  • The problem doesn't appear to be solely within the snippet: http://jsfiddle.net/udcktn1v/. Are you accessing the page via `http://`? If you're trying with `file://`, [browsers won't typically save cookies with it](http://stackoverflow.com/questions/335244/why-does-chrome-ignore-local-jquery-cookies). – Jonathan Lonowski Aug 27 '14 at 14:08
  • Actually im launching the application in local, could that be a reason? – Felix Aug 27 '14 at 14:55
  • Anyone? im convinced this is an easy thing to solve, it's just that i lack the knowledge about JQuery – Felix Aug 29 '14 at 07:01

0 Answers0