Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/m3talsmith/jquery-value-checkbox

A simple jQuery plugin that sets and toggles a checkboxes value. Very useful for those who need it.
https://github.com/m3talsmith/jquery-value-checkbox

Last synced: about 1 month ago
JSON representation

A simple jQuery plugin that sets and toggles a checkboxes value. Very useful for those who need it.

Awesome Lists containing this project

README

        

h1. Summary

Value Checkbox meets a very small specific need; the ability to change the value of check boxes to default values based on it's checked or unchecked state. There are no callbacks at the time and only three optional arguments.

I wrote this when I needed the value to change for a specific project, but first I looked around for a solution. It seems like no one either needs this, or has bothered to release something like this, so I'm releasing this in case the need arises for others.

h2. Usage

The only requirement is "jQuery":http://jquery.com/ .

The simplest way to use this is to just search for check boxes and attach valueCheckbox to it:

$("input[type='checkbox']").valueCheckbox();

This makes the assumption that it is running in strict mode, the checked value is true, and the unchecked value is false. You can turn strict mode off and it will assume that whatever state the check box is in, that's going to be the value for that state.

So, If you have a check box (named ".checked" for the sake of this demo) that is checked on the page when the page loads, and strict mode is turned off via:

$(".checked").valueCheckbox({strict:false});

The checked value will be whatever value the initial checked. Vice versa if the check box was unchecked, and so on.

h2. A note on queries

The plugin uses $.each() internally so it can handle an array of elements (like the first example) or a single element (like the second example). There is no need to do your own $.each() on the query results.

h2. Syntax and arguments

There are three arguments provided in hash form:

$("input[type='checkbox']").valueCheckbox({

strict: true,
checked_value: "yea",
unchecked_value: "nay"
});

And here's what all of that means ...

* *strict*: true or false
* *checked_value*: defaults to true but can be whatever you choose
* *unchecked_value*: defaults to false but can be whatever you choose

Strict mode will always be set the checked and unchecked values to true and false unless you provide it with other values. Strict mode is the default mode and must be manually turned off if you want it to automatically grab the checked or unchecked value.

h2. That is all

Feel free to for and add to this. I may add callbacks to this in order to help ajax requests along: it's highly probable. If you make any changes you feel should be in the core of this, feel free to send me a pull request; I love reading other people's code and learning new things!

Best regards world. Code on.