This version of the documentation is outdated. Check the latest version here!

Checkbox

Checkboxes are used when there are lists of options and the user may select any number of choices, including zero, one, or several. In other words, each checkbox is independent of all other checkboxes in the list, so checking one box doesn't uncheck the others.

Basic Usage

The following example will render a simple Checkbox with a label.

HTML
<label>
    <input type="checkbox" mbsc-checkbox data-label="The label of the Checkbox" />
</label>

Auto initialization

If the component is added later to the DOM, e.g. with an Ajax page load, a custom function named enhance needs to be called in order to initialize the dynamically added component. When the enhance function is called on a DOM element, all form elements will be initialized inside this element.

$.get('/myform', function (responseHtml) {
    var $page = $('#page');
    $page.html(responseHtml);
    mobiscroll.enhance($page[0]);
});

Passing options

All the options can be passed to the checkbox via data- attributes with exceptions of the native input supported ones. The attributes that the native html input supports can be passed directly without the data- prefix. For example thechecked or the disabled attributes (and so on...).

In this example we pass the rtl and the disabled options:

HTML
<label>
    <input type="checkbox" mbsc-checkbox data-rtl="true" disabled data-label="This is Disabled" />
</label>

Selecting programmatically

Selecting the value for a checkbox means setting the checked state of it. This can be done through the instance. For example:

HTML
<label>
    <input id="my-checked-checkbox" type="checkbox" checked mbsc-checkbox data-label="Initially checked" />
</label>
JS
// get the input element, the checkbox was initialized on
const inputElement = $('#my-checked-checkbox');
// get the instance of the Checkbox component
const checkboxInstance = mobiscroll.getInst(inputElement);

const checkedState = checkboxInstance.checked; // get the selected state as a boolean value
checkboxInstance.checked = false; // will deselect the checkbox

Disabled state

The underlying input's disabled attribute will be taken into account at initialization time. For example:

<!-- This will create a disabled checkbox -->
<label>
    <input type="checkbox" checked disabled mbsc-checkbox data-label="Initially selected and disabled" />
</label>

<!-- The following will not be disabled -->
<label>
    <input type="checkbox" checked mbsc-checkbox data-label="Initially selected and not disabled" />
</label>

Changing options dynamically

The options of the checkbox can be changed dynamically using the setOptions method on the instance itself, or the global setOptions method.
Here is an example on how to change the disabled option on a single checkbox:

HTML
<label>
    <input id="my-checkbox" type="checkbox" disabled mbsc-checkbox data-label="My checkbox" />
</label>
JS
// get the input element, the checkbox was initialized on
const inputElement = $('#my-checkbox');
// get the instance of the checkbox component
const checkboxInstance = mobiscroll.getInst(inputElement);

// set the disabled setting to false
checkboxInstance.setOptions({ disabled: false });

For many more examples - simple and complex use-cases - check out the checkbox demos for jquery.

Options

Name Data attribute Type Default value Description
checked data-checked Boolean undefined Sets the checked state of the checkbox.
color data-color String undefined A predefined color to style the component.
Supported values are:
  • primary
  • secondary
  • success
  • danger
  • warning
  • info
  • dark
  • light
description data-description String undefined A description that shows up under the label of the component.
disabled data-disabled Boolean false Initial disabled state of the component. This will take no effect in inline display mode.
label data-label String undefined Sets the label of component.
position data-position String 'end' Sets the position of the switch handle depending on the RTL setting. It can be 'start' or 'end'.
When in LTR mode, the 'start' will set the handle position to the left (a.k.a. from left to right the switch will start with the handle) and the 'end' will set it to the right - so the handle goes to the end of the switch.
In RTL mode, the 'start' will position the hande to the right. The 'end' will position the handle to the left in this case.
rtl data-rtl Boolean false Right to left display.
theme data-theme String undefined

Sets the visual appearance of the component.

If it is 'auto' or undefined, the theme will automatically be chosen based on the platform. If custom themes are also present, they will take precedence over the built in themes, e.g. if there's an iOS based custom theme, it will be chosen on the iOS platform instead of the default iOS theme.

Supplied themes:
  • 'ios' - iOS theme
  • 'material' - Material theme
  • 'windows' - Windows theme
It's possible to modify theme colors or create custom themes.
Make sure that the theme you set is included in the downloaded package.
themeVariant data-theme-variant String undefined

Controls which variant of the theme will be used (light or dark).

Possible values:
  • 'light' - Use the light variant of the theme.
  • 'dark' - Use the dark variant of the theme.
  • 'auto' or undefined - Detect the preferred system theme on devices where this is supported.

To use the option with custom themes, make sure to create two custom themes, where the dark version has the same name as the light one, suffixed with '-dark', e.g.: 'my-theme' and 'my-theme-dark'.

Customizing the appearance

While the provided pre-built themes are enough in many use cases, most of the times on top of adapting to a specific platform, you'd also like to match a brand or color scheme. Mobiscroll provides various ways to achieve this:

Override the Sass Color Variables

A convenient way to customize the colors of the Mobiscroll components is to override the Sass color variables.

Let's say your branding uses a nice red accent color, and you'd like that color to appear on the Mobiscroll components as well, while still using platform specific themes (e.g. ios on iOS devices, material on Android devices, and mobiscroll on desktop). You can override the accent color for every theme:

$mbsc-ios-accent: #e61d2a;
$mbsc-material-accent: #e61d2a;
$mbsc-mobiscroll-accent: #e61d2a;

@import "~@mobiscroll/JQuery/dist/css/mobiscroll.jquery.scss"
It's important that you override the variables BEFORE the scss file import, otherwise it won't make any difference.
Here's a complete guide on how to set up Mobiscroll with SASS support

You can also customize the colors on many levels:

  1. Theme specific variables (ex. $mbsc-material-background, $mbsc-ios-dark-text) are applied to all components in a theme. Complete list of variables here.
  2. Component specific global variables (ex. $mbsc-card-background-light, $mbsc-listview-text-dark) are applied to all themes for a specific component.
  3. Component and theme specific variables (ex. $mbsc-ios-dark-form-background, $mbsc-material-input-text) are applied to a specific theme and a specific component.

For many more examples - simple and complex use-cases - check out the checkbox demos for jquery.