Set cookie on click on button (with jQuery)

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

+4
source share
2 answers

This tutorial 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}); } }); 
+4
source

Based on Henridv's Answer , I ended up with this:

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

Please note that my solution also sets the cookie to false if the checkbox is unchecked. In other words, this saves the current cookie.

0
source

All Articles