Set cookie on checkbox click (with jquery)

Mark Henry picture Mark Henry · May 21, 2011 · Viewed 7.4k times · Source

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?

Answer

Henridv picture Henridv · May 21, 2011

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});
  }
});