Back to Top

How to use jQuery Cookie

jQuery Cookie Explained

jQuery provides a plugin which you can use the jQuery library itself and you can easily store and access cookies.

Before we begin, let’s say a few words about the jQuery Cookie plugin which is basically useful to store useful information.Today we will learn how to use jQuery Cookie to store some user information in a Cookie to display a specific content to the user.

SYNTAX:

$.cookie("name", "value","expires", "path","domain" ,"secure" );

About Parameters:

Name The “expires” option defines unique cookie name.
Value It defines the value of the cookie name you defined in first option
Expires The “expires” option defines active lifetime of the cookie. If omitted, the cookie is a session cookie.
path It defines the path where the cookie is to be used. If you want to make it available for the entire page use default path: ‘/’.
Domain Domain Name is the cookie was created.You can use a specific domain name or subdomain name.
Secure By Default it’s a false. If true, the cookie transmission requires a secure protocol (HTTPS).

Let’s understand by creating one application(demo) using jQuery Cookie plugin.Here, I am going to explain how you can set cookie and increment/decrement values of the cookie using the jQuery plugin.

First of include the jQuery library and after including the jQuery library, we will include the jquery.cookie.js plugin in HTML < head > tag. After that, include your JavaScript file or just place your code into a < script > tag.

// jquery library include


Next, we place jQuery script code which is used jQuery Cookie plugin and jQuery core elements.

$(document).ready(function(){       
    var Cookie = $.cookie( 'demoCookie' );
	// To get cookie value
        if(Cookie==null || Cookie=='')
        {
            $.cookie('demoCookie',0); 
           // To set cookie value to zero
        }  
     $('.button-prev').click(function() 
        {
                var Cookie = $.cookie( 'demoCookie' );
                Cookie=parseInt(Cookie)-1;
		// decrement cookie value to 1.
                $.cookie('demoCookie',Cookie);
        });
     $('.button-next').click(function() {
                var Cookie = $.cookie( 'demoCookie' );
                Cookie=parseInt(Cookie)+1;
		// increment cookie to 1
                $.cookie('demoCookie',Cookie);
        });
    $('.button-delete').click(function() {               
                $.cookie('demoCookie','');
		//empty cookie to revert back cookie value to null
        });
    $('.jq-text').text(Cookie).show(); 
      // To display cookie.
})

Now, place your HTML code into < body > tag which is basically a divs used to display cookie information stored into the cookies.


Following these simple steps, you can easily implement jQuery Cookie code.Let’s put this code together and see the demo how it works!!

DEMO

View Demo

Thanks for reading and feel free to share your thoughts! Don’t Forget to Follow us on Twitter or Subscribe us to Get the Latest Updates.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Most Popular Posts

The Readers’ Poll – March 2013

Posted on 6 years ago

Bhumi

How to use session in Magento

Posted on 6 years ago

Bhumi

How to Setup WordPress Multisite Network?

Posted on 5 years ago

Bhumi

The Readers’ Poll – July 2013

Posted on 6 years ago

Bhumi

How To use Singleton Design Pattern in PHP

Posted on 7 years ago

Bhumi

Hello world

Updated 5 days ago

Nick Carter

In depth Understanding of Laravel Core

Updated 2 months ago

Bhumi

ECMAScript 6 New Features – 2

Updated 1 year ago

Bhumi

The Reader’s Poll – June 2015

Posted on 4 years ago

Bhumi

The Reader’s Poll – August 2014

Posted on 4 years ago

Bhumi

The Reader’s Poll – June 2014

Posted on 5 years ago

Bhumi

The Readers’ Poll – May 2014

Posted on 5 years ago

Bhumi

The Readers’ Poll – August 2013

Posted on 5 years ago

Bhumi