Bootstrap Checkbox X bootstrap-checkbox-x

Thankful to Krajee!
to get more out of us.

NOTE: The plugins on this site, strive to use a lot of CSS3 and HTML5 features in addition to JQuery. Hence, one may find either CSS3, HTML5 or a mix of both to achieve a plugin's requirements in many implementations.

An extended checkbox plugin for Bootstrap built using JQuery, which allows three checkbox states and includes additional styles. The plugin uses Bootstrap markup and CSS 3 styling by default, but it can be overridden with any other CSS markup. It also helps you to fallback to a native checkbox OR display native checkboxes with tristate capability.View a complete demo.

With v1.5.4, the plugin now supports initializing widget on a SELECT input as well (other than a CHECKBOX or TEXT input).

Tip

Not seeing the updated content on this page! Hard refresh your browser to clean cache for this page (e.g. SHIFT-F5 on Windows Chrome).

Note

The latest version of the plugin v1.5.4 has been released. Refer the CHANGE LOG for details.
  1. Enhances any HTML input (preferrably text input) with data-toggle=checkbox-x to an extended checkbox control.

  2. The plugin offers the following three states and values for the checkboxes:

    • 1: Checkbox is checked. This value is configurable through valueChecked property.

    • 0: Checkbox is unchecked. This value is configurable through valueUnchecked property.

    • null: Checkbox is indeterminate. This value is configurable through valueNull property.

  3. You can set the plugin to allow three states or the default two states for the checkbox.

  4. Specifically uses Bootstrap 3.x styles & glyphs. One can configure the checked, unchecked, and indeterminate icons to be shown for the checkboxes.

  5. Special CSS 3 styling, to enhance the control to look like any Bootstrap 3 form control. Supports the has-error, has-success, has-warning styling states like other Bootstrap form-controls.

  6. Plugin CSS styling automatically defaults the checkboxes to inline display. You can also control the markup for block display like in checkbox lists.

  7. You can add a label before or after with a for attribute and click on the label to change the checkbox values. Alternatively you can enclose the input within a label tag as well.

  8. Ability to navigate to the checkbox controls via keyboard, and modify the values using the space bar on the keyboard.

  9. Ability to size the checkbox control. Five prebuilt size templates are available xl, lg, md, sm, and xs.

  10. Triggers JQuery events for advanced development. The plugin automatically triggers the change event for the input, whenever the checkbox value is changed via clicking. Events currently available are change and checkbox.reset.

  11. Ability to access methods and refresh the input dynamically via javascript at runtime.

  12. Disabled and readonly checkbox input support.

  13. Allow third state to be configured (only on init) for two state checkboxes.

  14. Added support for displaying native checkboxes with tristate capability. When displaying native checkboxes, the advanced styling offerred by the plugin will not be available.

Note

The checkbox plugin stores the values as character format (and not boolean format) for checked and unchecked states. This can be overridden by setting valueChecked, valueUnchecked and valueNull properties.

  1. Latest JQuery

  2. Most modern browsers supporting CSS3, HTML5 inputs, & JQuery.

  3. Bootstrap 3.x is optional and used by default, but can be overridden to use any CSS.

The plugin can be installed automatically or manually using one of these options:

Bower Package Manager

Installation via bower package manager is as simple as running:
$ bower install bootstrap-checkbox-x

Composer Package Manager

You can install bootstrap Checkbox X via composer package manager. Either run

$ php composer.phar require kartik-v/bootstrap-checkbox-x "dev-master"

or add:

"kartik-v/bootstrap-checkbox-x": "dev-master"

to your composer.json file

Manual Install

You can also manually install the plugin easily to your project. Just download the source ZIP or TAR ball and extract the plugin assets (css and js folders) into your project.

Step 1: Load the following assets in your header.

<link href="http://netdna.bootstrapcdn.com/bootstrap/3.1.0/css/bootstrap.min.css" rel="stylesheet">
<link href="path/to/css/checkbox-x.min.css" media="all" rel="stylesheet" type="text/css" />
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<script src="path/to/js/checkbox-x.min.js" type="text/javascript"></script>
<!-- optional if you are using themes -->
<link href="path/to/css/theme-krajee-flatblue.min.css" media="all" rel="stylesheet" type="text/css" />

If you noticed, you need to load the jquery.min.js and bootstrap.min.css in addition to the checkbox-x.min.css and checkbox-x.min.js. Loading the theme file theme-krajee-flatblue.min.css is optional and is needed if you want to override the default theme. Read the theme property of the plugin for understanding configuration details for themes.

Step 2a: Initialize the plugin on your page. For example,

<input id="input-id">
<script>
$(document).on("ready", function() {
    // initialize with defaults
    $("#input-id").checkboxX();

    // with plugin options
    $("#input-id").checkboxX({threeState: true, size:'lg'});
});
</script>

The #input-id is the identifier for the input on your page, which is hidden automatically by the plugin.

Step 2b: Alternatively, you can directly set the plugin options to any input, through HTML 5 data attributes to your input field.

<"input-id" value="1" data-toggle="checkbox-x" data-three-state="true" data-size="lg">

Note

  • The plugin will automatically convert inputs with data-toggle set to checkbox-x to a Checkbox X control.
  • You can add the CSS class cbx-label to the labels for your checkbox inputs to style them similar to bootstrap's checkbox labels (with additional enhancements for contextual colors).

View a complete demo.

The plugin supports these following options:

theme

string, the name of the theme to be used. This will automatically append the cbx-themeName CSS class to the checkbox container and can be used for overriding the styles. Defaults to empty string. The plugin includes an additional theme named krajee-flat. The CSS for this theme file is included in theme-krajee-flatblue.css in the CSS folder. If you set theme to krajee-flatblue, the CSS class cbx-krajee-flatblue will automatically be appended to the checkbox container. Note that the theme CSS file must be loaded after checkbox-x.css on the page.

threeState

boolean whether to use three states for the checkbox. This will allow you to save null values, in addition to the built in 1 and 0 values. Defaults to true. If set to false, it will allow only the default two values: 1 and 0.

allowThreeValOnInit

boolean whether to allow three values for two state checkboxes on initialization (i.e. when threeState is false. Defaults to false. If set to true for a two state checkbox, one can also have an indeterminate (empty) value displayed on initialization.

inline

boolean, whether the checkboxes are to be styled in an inline-block with other elements placed before or after. Defaults to true. If set to false, checkboxes will be displayed as a block (like in checkbox lists).

iconChecked

string, the html markup for the glyph / icon to be displayed when the checkbox is checked. Defaults to <i class="glyphicon glyphicon-ok"></i>.

iconUnchecked

string, the html markup for the glyph / icon to be displayed when the checkbox is unchecked. Defaults to a blank character .

iconNull

string, the html markup for the glyph / icon to be displayed when the checkbox is null. Defaults to <div class="cbx-icon-null"></div>.

valueChecked

int|string, the value when the checkbox is checked. Defaults to string '1'.

valueUnchecked

int|string, the value when the checkbox is unchecked. Defaults to string '0'.

valueNull

int|string, the value when the checkbox is indeterminate (for three state). Defaults to empty string ''.

size

string size of the checkbox control. Should be one of xl, lg, md, sm, or xs. Defaults to md.

enclosedLabel

boolean, whether the checkbox input is enclosed inside a label. Defaults to false. Setting it to true is important if you have enclosed the base input inside a label tag.

useNative

boolean, whether to display the native checkbox control instead of the advanced styled input generated by the plugin. Defaults to false. This property will be applied only if your source element is a checkbox input. Setting it to true will allow you to use a threeState native checkbox. Note that when you have set useNative to true, you will lose the advanced styling features offerred by bootstrap-checkbox-x.

tabindex

int, the tabindex property for the checkbox input. Defaults to `1000`. If set to empty, `0` or `false`, no tabindex will be assigned. You can set it to `-1` if you need to navigate first on the input via the tab key.

The plugin supports these events:

change

This event is triggered when the Checkbox X is modified or changed:

Example:

$('#input-id').on('change', function() {
    console.log('checkbox changed');
});

resetinput.checkbox

This event is triggered when the rating is reset using the reset method or by resetting the form.

Example:

$('#input-id').on('resetinput.checkbox', function(event) {
    console.log("resetinput.checkbox");
});

The plugin supports these methods:

refresh

Use this method to dynamically refresh the checkbox control options via javascript after the plugin has been initialized. The method accepts the plugin options entered as an array.

// Example: Call the method below in checkbox control.change event to disable the checkbox control and
// change the size to 'xl'.
$('#input-id').attr('disabled', 'disabled');
$('#input-id').checkboxX('refresh', {size: 'xl'});

reset

Reset the checkbox control to the initial value. For example after a form reset.

$('#input-id').checkboxX('reset');

destroy

Use this method to destroy the plugin and revert back the native input to original state.

$('#input-id').checkboxX('destroy');

The plugin has been implemented as extensions in the following frameworks

Sl.FrameworkExtension SourceDeveloped ByExtension Demo
1.Yii Framework 2.0Yii2 Checkbox XKrajee Yii2 Checkbox X

Do you know any other framework extension using this plugin which is not listed here? Tell us so that we can consider its inclusion in this list.

bootstrap-checkbox-x is released under the BSD 3-Clause License. See the bundled LICENSE.md for details.