> For the complete documentation index, see [llms.txt](https://docs.wem.io/platform/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.wem.io/platform/tutorials/building-widgets/wemscript/8-types/1-boolean.md).

# 1. Boolean

A boolean variable can be set using the keywords `true`, `false`, `unknownboolean`, or a WEM expression that returns a boolean type.

```
var @a: boolean
@a := true
@a := false
@a := unknownboolean
@a := 3.1415 <> 2.7182
```

When performing a postback, it is essential to send the text "true", "false", or an empty string "" to update the property to the respective values of `true`, `false`, and `unknownboolean`. This requires careful attention when sending data back.

Let’s examine some examples and analyze them.

All examples feature a writable boolean data model property called `BooleanProperty`.

#### Boolean Textfield Postback Example

```html
<? register input @BooleanProperty ?>
<input type="text" name="<?attr OutputId(@BooleanProperty) ?>" value="<?attr @BooleanProperty ?>">
```

Aside from the poor user experience, this approach does not work because when the portal language is set to English, `<?attr @BooleanProperty ?>` will output "yes" when `@BooleanProperty` is `true` and "no" when it is `false`. These outputs are invalid boolean values for a postback. As a workaround, you could use `<?js @BooleanProperty ?>` instead, as it will output `true`, `false`, and `unknownboolean` as "true", "false", and "null" respectively. However, "null" is an invalid value, so it will be interpreted as `unknownboolean`. But please keep this workaround confidential.

Since we typically do not create a boolean text field, let’s explore more common boolean input fields.

#### Radio Buttons Postback Example

```html
<? register input @BooleanProperty ?>
<span>
	This statement is?
</span>
<label>
	True: 
	<input type="radio" name="<?attr OutputId(@BooleanProperty) ?>" value="true" <? if @BooleanProperty ?>checked<? end ?>>
</label>
<label>
	False:
	<input type="radio" name="<?attr OutputId(@BooleanProperty) ?>" value="false" <? if not @BooleanProperty ?>checked<? end ?>>
</label>
<label>
	Unknown:
	<input type="radio" name="<?attr OutputId(@BooleanProperty) ?>" value="" <? if IsUnknown(@BooleanProperty) ?>checked<? end ?>>
</label>
```

In this example, we register the input `@BooleanProperty` and create three radio buttons with the text values "true", "false", and an empty string "" to set the value to `unknownboolean`. We check the current state of `@BooleanProperty` and add the `checked` attribute if it corresponds to the option value. Note that if `@BooleanProperty` is unknown, checking whether it is true will always return `false`, so we do not need to use `IsKnown(@BooleanProperty)` for the "true" and "false" options.

#### Checkbox (True/Unknown) Postback Example

```html
<? register input @BooleanProperty ?>
<label>
	<input type="checkbox" name="<?attr OutputId(@BooleanProperty) ?>" value="true" <? if @BooleanProperty ?>checked<? end ?>>
	Is the checkbox not checked?
</label>
```

This checkbox will set `@BooleanProperty` to either `true` or `unknownboolean`. This behavior is due to how the browser sends post data. If the checkbox is not checked, the browser will ignore it, resulting in the property assigned to this checkbox being set to `unknownboolean`. This may not always be the desired outcome, so let’s look at an improved version to address this issue.

#### Checkbox (True/False) Postback Example

```html
<? register input @BooleanProperty ?>
<label>
	<input type="hidden" id="<?attr OutputId(@BooleanProperty) ?>-hidden" name="<?attr OutputId(@BooleanProperty) ?>">	
	<input type="checkbox" id="<?attr OutputId(@BooleanProperty) ?>-checkbox" <? if @BooleanProperty ?>checked<? end ?>>
	Is the checkbox not checked?
</label>
<? submitscript ?>
	const checkboxEl = document.getElementById(<?js OutputId(@BooleanProperty) ?> + "-checkbox");
	const hiddenEl = document.getElementById(<?js OutputId(@BooleanProperty) ?> + "-hidden");
	hiddenEl.value = checkboxEl.checked ? "true" : "false";
<? end ?>
```

In this example, we introduce a hidden input field that will be used to send the data back, along with an intermediate checkbox to read the current checked state in the `submitscript`. Depending on whether the checkbox is checked, we set the hidden input field to "true" or "false".

Regardless of the type of widget you are building, even a simple boolean can become complicated if not carefully considered.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.wem.io/platform/tutorials/building-widgets/wemscript/8-types/1-boolean.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
