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
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});
}
});
2 Comments
Mark Henry
Ok, looking good. How do I echo the value in the username field?
Henridv
How do you mean? The value of the username field is retrieved by
$("$username").val().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.