Code Monkey home page Code Monkey logo

jquery-cookies's Introduction

jquery-cookies

Adds JAAulde/cookies to the jQuery namespace, and provides jQ extension methods to bind cookies to form values and html elements.

installation

bower install jaaulde-jquery-cookies
npm install jaaulde-jquery-cookies

html

Download the code (requires JAAulde/cookies), link it in your HTML file.

<script src="/path/to/jaaulde-cookies.js"></script>
<script src="/path/to/jaaulde-cookies.jquery.js"></script>

usage

Cookie methods

The entire cookies singleton API from JAAulde/cookies is aliased in the jQuery namespace. For example, cookies.set() is available as $.cookies.set(). For a list of all available cookie methods and their docs, see JAAulde/cookies documentation.

jQuery plugin methods

$.fn.cookify

Write an element's contents to a cookie. Options which can be specified are the JAAulde/cookies cookie options.

signature
/**
 * $('selector').cookify - set the value of an input field, or the innerHTML of an element, to a cookie by the name or id of the field or element
 *                           (field or element MUST have name or id attribute)
 *
 * @access public
 * @param options OBJECT - list of cookie options to specify
 * @see https://github.com/JAAulde/cookies#cookie-options
 * @return jQuery
 */
cookify: function (options) {
example
$('#username').cookify();

This code, given an input such as:

<input type="text" id="username" name="username" value="" />

will read the value from the input and write it to a cookie by the name of username.

If, instead of an input, the selector is for an element such as:

<div id="username"></div>

the element's innerHTML will be written to the cookie.

$.fn.cookieFill

Fill an element with the value of a cookie by the same name

signature
/**
 * $('selector').cookieFill - set the value of an input field or the innerHTML of an element from a cookie by the name or id of the field or element
 *
 * @access public
 * @return jQuery
 */
cookieFill: function () {
example
$('#username').cookieFill();

This code, given an input such as:

<input type="text" id="username" name="username" value="" />

will read the value of a cookie by the name of username and write it to the input's value.

If, instead of an input, the selector is for an element such as:

<div id="username"></div>

the cookie's value will be written to the element's innerHTML.

$.fn.cookieBind

Fill an input using $.fn.cookieFill, then bind its change event to update a cookie via $.fn.cookify. Options which can be specified are the JAAulde/cookies cookie options.

signature
/**
 * $('selector').cookieBind - call cookieFill() on matching input elements, and bind their change events to cookify()
 *
 * @access public
 * @param options OBJECT - list of cookie options to specify
 * @see https://github.com/JAAulde/cookies#cookie-options
 * @return jQuery
 */
cookieBind: function (options) {
example
$('#username').cookieBind();

An important note:

The plugins use name and id attributes (listed in order of precedence, respectively) from the matched elements as the name of the cookie on which to operate.

So, given an element with a different name than id such as:

<input type="text" id="foo" name="bar" value="" />

The plugins will try to find and use a cookie named bar. If that cookie does not exist, a cookie named foo will be sought. This is true no matter what selector was used to find the element--each element is looked at independently and its attributes are iterated in this order to find an applicable cookie.

jquery-cookies's People

Contributors

jaaulde avatar

Stargazers

sun avatar  avatar  avatar James Moberg avatar

Watchers

 avatar

jquery-cookies's Issues

cookies alias

Is it just me or is cookies not being aliased into the jQuery namespace an mentioned?

The entire cookies singleton API from JAAulde/cookies is aliased in the jQuery namespace. For example, cookies.set() is available as $.cookies.set().

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.