Store Multiple Checkbox state from cookie using Jquery
In this tutorial we have explain how to store cookie using the Query. some times we need to store multiple state of checkbox in cookie and need to retrieve it from user browser when the current page is expire or user can visit next time then cookie are best option to retrieve the browser data after ones store in the browser
HTML Code :
<div id=”dvChkBox”>
<input id=”high” name=”high” type=”checkbox” /> high
<input id=”low” name=”low” type=”checkbox” /> low
<input id=”open” name=”open” type=”checkbox” /> open
<input id=”close” name=”close” type=”checkbox” /> close
</div>
Now here we can create and read cookie with the multiple checkbox using the jquery. first create cookie with setCookie() and read it from the getCookie() function.
Cookie value set using the synatax :
setCookie(‘cookiename’, options,365);
here first parameter is cookiename and second parameter is cookievalue and in third parameter we can set the cookie expire time whatever we can want.
For Create and Read multiple cookie value jquery code is below.
<script type=”text/javascript”>
$(function () {
var $ck = $(“#dvChkBox input:checkbox”); // here we can find the checkbox from the div
$ck.click(function () { //when click on checkbox it can store in cookie
var $chkboxes = $(“#dvChkBox input:checkbox”);
var options = $chkboxes.map(function () {
if (this.checked) return this.name;
}).get().join(‘,’);
// here we can set cookie nane,cookie value and cookie expire time
setCookie(‘cookiename’, options,365);
});
});
var $chkboxes = $(“#dvChkBox input:checkbox”);
$chkboxes.change(setCookie); // when the checkbox state is change than set the cookie value
$(document).ready(function () {
var ckvalue = getCookie(‘cookiename’).split(‘,’);
$chkboxes.prop(‘checked’, false);
for (var $m in ckvalue) {
$(“input[name='” + ckvalue[$m] + “‘]”).prop(‘checked’, true);
}
});
// Here using setCookie() function we store cookie value and cookie expire time
function setCookie(ckname, cvalue, exdays) {
var d = new Date();
d.setTime(d.getTime() + (exdays * 24 * 60 * 60 * 1000));
var expires = “expires=” + d.toUTCString();
document.cookie = ckname + “=” + cvalue + “; ” + expires;
}
// Here using getCookie() function we can read cookie value
function getCookie(ckname) {
var name = ckname + “=”;
var ca = document.cookie.split(‘;’);
for (var i = 0; i < ca.length; i++) {
var c = ca[i];
while (c.charAt(0) == ‘ ‘) c = c.substring(1);
if (c.indexOf(name) == 0) return c.substring(name.length, c.length);
}
return “”;
}
</script>
Store Multiple Checkbox state from cookie using Jquery, this code is tested and working well as per my implementation, and if you have any query regarding code then feel free to comment on comment section.
SEE MORE
- Auto Refresh Partial View in ASP.NET MVC
- What is ASP.NET Core
- Difference between TempData keep() And Peek() in Asp.Net MVC
- Difference between viewbag,viewdata and tempdata in asp.net mvc
- ASP.NET MVC With AngularJS
- Retrieving Data Using Form Collection and Inserting Into ASP.Net MVC
- MVC CRUD Operations Using Entity Framework
- Search Functionality in ASP.NET MVC
- How to create a User Registration page using asp.net mvc
- Store Multiple Checkbox state from cookie using Jquery
- Cascading Dropdownlist using Ajax in Asp.Net Mvc with city state country
- Insert, Update, Delete In GridView Using ASP.Net C#
- Binding Dropdownlist With Database In Asp.Net MVC
- Search and Filter data in Gridview using Asp.net MVC
- Select Insert, Update And Delete With ASP.NET MVC
- Display Data in GridView Using Asp.net MVC
- Validation in ASP.NET MVC Razor view
- CRUD Operation Using 3-Tier Architecture In ASP.NET
- How to get Connection String from Web.Config in Asp.Net C#
- Login page using 3-Tier Architecture in ASP.Net
- Asp.Net Image Upload in 3-Tier Architecture and store in sql database
5 Comments