Stay organized with collections
Save and categorize content based on your preferences.
A checkbox field stores a string as its value, and a string as its text. Its
value is either 'TRUE' or 'FALSE', and its text is either 'true' or
'false'.
The checkbox constructor takes in an optional value and an optional
validator. The optional value should be either
'TRUE', 'FALSE', or a boolean, otherwise it will default to false.
Serialization
JSON
The JSON for a checkbox field looks like so:
{"fields":{"FIELDNAME":true}}
Where FIELDNAME is a string referencing a checkbox field, and
the value is the value to apply to the field. The value must be a boolean.
XML
The XML for a checkbox field looks like so:
<fieldname="FIELDNAME">TRUE</field>
or
<fieldname="FIELDNAME">true</field>
Where the name attribute contains a string referencing an checkbox field,
and the inner text is the value to apply to the field. The inner text value
follows the same rules as the constructor value.
Note that after being deserialized and reserialized all of the inner text
values will be in caps ('TRUE' or 'FALSE'). This is sometimes important
when diffing workspaces.
Customization
Checkmark character
The Blockly.FieldCheckbox.CHECK_CHAR property can be used to change what the
checkmark looks like. The value should be a string containing a unicode
character.
The CHECK_CHAR property defaults to '\u2713' or ✓.
This is a global property, so it will modify all checkbox fields when set.
Creating a checkbox validator
A checkbox field's value is either 'TRUE' or 'FALSE' so a validator should
accept those values (i.e. a string) and return 'TRUE', 'FALSE', null, or
undefined.
Here's an example of a validator that hides or shows a text input field based on
whether the checkbox is checked:
[null,null,["Last updated 2025-06-17 UTC."],[[["\u003cp\u003eCheckbox fields store a string value (\u003ccode\u003e'TRUE'\u003c/code\u003e or \u003ccode\u003e'FALSE'\u003c/code\u003e) and a corresponding text representation (\u003ccode\u003e'true'\u003c/code\u003e or \u003ccode\u003e'false'\u003c/code\u003e).\u003c/p\u003e\n"],["\u003cp\u003eCheckbox fields can be easily created and customized using JSON or JavaScript within Blockly.\u003c/p\u003e\n"],["\u003cp\u003eSerialization of checkbox fields is supported in both JSON and XML formats, ensuring data persistence.\u003c/p\u003e\n"],["\u003cp\u003eThe checkmark character in checkbox fields can be globally customized using the \u003ccode\u003eBlockly.FieldCheckbox.CHECK_CHAR\u003c/code\u003e property.\u003c/p\u003e\n"],["\u003cp\u003eValidators can be implemented for checkbox fields to control behavior based on their state, such as dynamically showing/hiding other fields.\u003c/p\u003e\n"]]],["Checkbox fields store either `'TRUE'` or `'FALSE'` as their value and `'true'` or `'false'` as text. They can be created via JSON or JavaScript. Serialization in JSON uses boolean values, while XML uses `TRUE` or `FALSE` strings. Validators can modify the field's behavior, accepting and returning `'TRUE'` or `'FALSE'`. The checkmark character is customizable globally. The `getValueBoolean` method should not be used within validators.\n"],null,["# Checkbox fields\n\nA checkbox field stores a string as its value, and a string as its text. Its\nvalue is either `'TRUE'` or `'FALSE'`, and its text is either `'true'` or\n`'false'`.\n\n#### Checkbox field\n\n!\\[[A block with the label \"checkbox:\" and a checkbox field with a check\nmark.](/static/blockly/images/fields/checkbox/on_block.png)\n\n#### Checkbox field on collapsed block\n\nCreation\n--------\n\n### JSON\n\n {\n \"type\": \"example_checkbox\",\n \"message0\": \"checkbox: %1\",\n \"args0\": [\n {\n \"type\": \"field_checkbox\",\n \"name\": \"FIELDNAME\",\n \"checked\": true\n }\n ]\n }\n\n### JavaScript\n\n Blockly.Blocks['example_checkbox'] = {\n init: function() {\n this.appendDummyInput()\n .appendField('checkbox:')\n .appendField(new Blockly.FieldCheckbox(true), 'FIELDNAME');\n }\n };\n\nThe checkbox constructor takes in an optional value and an optional\n[validator](#creating_a_checkbox_validator). The optional value should be either\n`'TRUE'`, `'FALSE'`, or a boolean, otherwise it will default to `false`.\n\nSerialization\n-------------\n\n### JSON\n\nThe JSON for a checkbox field looks like so: \n\n {\n \"fields\": {\n \"FIELDNAME\": true\n }\n }\n\nWhere `FIELDNAME` is a string referencing a checkbox field, and\nthe value is the value to apply to the field. The value must be a boolean.\n\n### XML\n\nThe XML for a checkbox field looks like so: \n\n \u003cfield name=\"FIELDNAME\"\u003eTRUE\u003c/field\u003e\n\nor \n\n \u003cfield name=\"FIELDNAME\"\u003etrue\u003c/field\u003e\n\n| **Note:** Quotes do not need to be applied to the inner text.\n\nWhere the `name` attribute contains a string referencing an checkbox field,\nand the inner text is the value to apply to the field. The inner text value\nfollows the same rules as the constructor value.\n\nNote that after being deserialized and reserialized all of the inner text\nvalues will be in caps (`'TRUE'` or `'FALSE'`). This is sometimes important\nwhen diffing workspaces.\n\nCustomization\n-------------\n\n### Checkmark character\n\nThe `Blockly.FieldCheckbox.CHECK_CHAR` property can be used to change what the\ncheckmark looks like. The value should be a string containing a unicode\ncharacter.\n\nThe `CHECK_CHAR` property defaults to '\\\\u2713' or ✓.\n\nThis is a global property, so it will modify all checkbox fields when set.\n\nCreating a checkbox validator\n-----------------------------\n\n| **Note:** For information on validators in general see [Validators](/blockly/guides/create-custom-blocks/fields/validators).\n\nA checkbox field's value is either `'TRUE'` or `'FALSE'` so a validator should\naccept those values (i.e. a string) and return `'TRUE'`, `'FALSE'`, `null`, or\n`undefined`.\n| **Caution:** the `getValueBoolean` method should not be used inside of validators, because it returns based on the current value, not the new value.\n\nHere's an example of a validator that hides or shows a text input field based on\nwhether the checkbox is checked: \n\n validate: function(newValue) {\n var sourceBlock = this.getSourceBlock();\n sourceBlock.showTextField_ = newValue == 'TRUE';\n sourceBlock.updateTextField();\n\n return newValue;\n },\n\n updateTextField: function() {\n var input = this.getInput('DUMMY');\n if (this.showTextField_ && !this.getField('TEXT')) {\n input.appendField(new Blockly.FieldTextInput(), 'TEXT');\n } else if (!this.showTextField_ && this.getField('TEXT')) {\n input.removeField('TEXT');\n }\n }\n\n#### Checkbox field with a validator"]]