Giter Club home page Giter Club logo

js-selectallcheckbox's Introduction

SelectAllCheckbox v1.0

See LICENSE for this software's licensing terms.

SelectAllCheckbox is a JavaScript library which makes it easy to create "select all" checkboxes for checkbox groups.

This library is a vanilla JavaScript port of my jQuery SelectAllCheckbox plugin.

Differences between this and the jQuery version

  • This library is incompatible with all versions of IE. I do projects like this to learn, and I wanted to use ES6 features. (I tested in Chromium, Firefox and legacy Edge, and those were OK).
  • The use of indeterminate checkboxes can no longer be disabled via configuration
  • Minor API differences, though conceptually it's the same (see the Using section)

Features

  • Grants a checkbox control over a group of checkboxes to select or deselect all of them
  • When checkboxes are individually checked/unchecked, the select-all checkbox's state updates accordingly between not checked, indeterminate (partially checked) and checked
  • Supports any number of checkbox groups
  • Configuration accepts a callback which executes when one or more checkboxes changes state
  • The callback receives the changed checkbox(es) as an array of DOM objects, plus the status of the checkbox group (all/some/none checked)
  • Select-all checkboxes do not modify the state of disabled checkboxes. Disabled checkboxes do affect the state of the select-all checkbox.

Using

Class: CheckboxGroup

Class Constants

Property Description
CheckboxGroup.GROUP_STATE_NONE Reflects the state of the checkbox group; a string with value none
CheckboxGroup.GROUP_STATE_SOME Reflects the state of the checkbox group; a string with value some
CheckboxGroup.GROUP_STATE_ALL Reflects the state of the checkbox group; a string with value all

Constructor Arguments

Property Description
selectAllId The HTML "id" attribute of the select-all checkbox
groupName The HTML "name" attribute of the checkboxes. Note: the select-all checkbox should not have a "name" attribute.
onChangeCallback A callback to execute when a checkbox's state changes

The callback function receives two arguments:

  1. The affected checkboxes as an array of DOM objects
  2. A string representing how many checkboxes in the group are checked; see CheckboxGroup.GROUP_STATE_NONE, CheckboxGroup.GROUP_STATE_SOME and CheckboxGroup.GROUP_STATE_ALL.

Example

let group1 = new CheckboxGroup(
   "selectAllButton",
   "checkboxGroup",
   function( checkboxes, checkedState ) { do stuff; }
);

See demo/demo.html file for a more thorough demo.

If you change a checkbox's state via script (for example, with document.getElementById("box1").checked = true, you must manually fire the change event on the checkbox to ensure that the select-all checkbox's state is updated.

js-selectallcheckbox's People

Contributors

kloverde avatar

Stargazers

 avatar

Watchers

 avatar  avatar

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.