4

I have a login form for which I would like to store the username (input field: username) in a cookie via Jquery the moment I click a checkbox ('remmber me'). Does anyone know how to do this?

2 Answers 2

4

This guide shows how to use the Cookie plugin for jQuery: http://www.electrictoolbox.com/jquery-cookies/

Then you can use something like this in your javascript files:

$(function() {
  $(".rememberme").change(function() {
     if ($(this).is(":checked"))
       $.cookie("loggin", $("#username").val(), {expires: 7});
  }
});
Sign up to request clarification or add additional context in comments.

2 Comments

Ok, looking good. How do I echo the value in the username field?
How do you mean? The value of the username field is retrieved by $("$username").val().
0

Building upon Henridv's answer, I ended up doing this:

$("#remember_me").change(function() {
    $.cookie("remember_me",
             $(this).is(":checked"),
             {expires: 7, path:'/'}
    );
});

Note that my solution also sets the cookie to false, if the checkbox is deselected. In other words, this keeps the cookie current.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.