Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Home

m3talsmith edited this page · 4 revisions
Clone this wiki locally

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.

Usage

The only requirement is jQuery .

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.

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.

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.

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.

Something went wrong with that request. Please try again.