DoneJS StealJS jQuery ++ FuncUnit DocumentJS
3.0.0
2.3.27

 

  • Github
  • Twitter
  • Chat
  • Forum
  • Guides
  • Core
  • Ecosystem
    • can-construct-super
    • can-define-stream
    • can-fixture
    • can-fixture-socket
    • can-jquery
    • can-stache-converters
      • Pages
        • select[multiple]
        • input[type=checkbox]
        • input[type=radio]
        • select
      • Converters
        • boolean-to-inList
        • either-or
        • equal
        • index-to-selected
        • not
        • string-to-any
    • can-stream
    • can-vdom
    • can-view-import
    • can-zone
    • steal-stache
  • Infrastructure
  • Legacy
  • Bitovi
    • Bitovi.com
    • Blog
    • Consulting
    • Training
    • Open Source
  • Chat
  • Forum
  • Star
  • Follow @canjs
  • CanJS
  • /
  • Ecosystem
  • /
  • can-stache-converters
  • /
  • input[type=checkbox]
  • / On this page
    • input[type=checkbox]

      page

      Cross bind a value to a checkbox.

      • source

      Binding to checkboxes

      To bind to a checkbox and set a boolean value within your scope, set up a two way binding to the input's checked property like so:

      <input type="checkbox" {($checked)}="val" />
      

      can-stache-converters provides a couple of convenient converters that handle common use cases for binding to a checkbox.

      Binding based on whether an item is in a list

      Using boolean-to-inList is useful to two-way bind to a checkbox based on whether an item is in a list or not. When the checkbox is checked/unchecked, the list will be updated and that item will either be removed or added to the list.

      Binding based on a binary decision

      An alternative true and false value can be specified by using [can-stache.converters.either-or]. This is used for setting up a "boolean" property that only has two possible valid values, whose values are modelled by the true/false checked property of a checkbox, as in the following example:

      <input type="checkbox" {($checked)}="either-or=(~val, 'a', 'b')" />
      

      In this case, the data passed in as val is a can-compute that contains either the value a or b. If the value of val is a then the checkbox will be checked. When the user checks/unchecks the checkbox then the value of val is set to be either a or b depending on whether it is checked.

      CanJS is part of DoneJS. Created and maintained by the core DoneJS team and Bitovi. Currently 3.0.0.