Last updated: 15 Apr 20 16:28:23 (UTC)

checkboxes and arrays

<<checkbox variable_name unchecked_value checked_value [checked]>>

Creates a checkbox, used to modify the value of the $variable with the given name.

> See Also:>  > > Interactive macro warning> .

> Arguments:

  • > variable_name:>  The name of the variable to modify, which *> must*>  be quoted (e.g. > "variabletomodify,which >must> bequoted(e.g. >"variable to modify, which *> must*>  be quoted (e.g. > "foo"> ). Object and array property references are also supported (e.g. > “foo.bar"> , > "foo.bar">, >"foo.bar"> , > "foo[‘bar’]”> , & > “$foo[0]”> ).
  • > unchecked_value:>  The value set by the checkbox when unchecked.
  • > checked_value:>  The value set by the checkbox when checked.
  • > checked:>  (optional) Keyword, used to signify that the checkbox should be in the checked state.

> Usage:

What pies do you enjoy?

  • Blueberry? <<checkbox “$pieBlueberry” false true checked>>
  • Cherry? <<checkbox “$pieCherry” false true>>
  • Coconut cream? <<checkbox “$pieCoconutCream” false true checked>>

Example: I wanted to have a list of checkboxes, and then create dynamic/customized text in later passages based on what the user selected from those boxes. So in the checkbox passage, I put something like the following passage:

<<set $resources to []>>

<<checkbox "$air" N air>> Air Travel
<<checkbox "$car" N car>> Car Travel
<<checkbox "$train" N train>> Train Travel

[[results]]
<<set $resources to []>>

<<checkbox "$air" N air>> Air Travel
<<checkbox "$car" N car>> Car Travel
<<checkbox "$train" N train>> Train Travel

[[results]]

Where $resources is set to an array.

To require at least one box be checked (you can use <> or <>):

<<button "RESULTS">>

<<if $airTrav is "N" and $carTrav is "N" and $trainTrav is "N" and $minerals is "N" and $electricity is "N" and $shipping is "N" and $nonlocalFood is "N" and $peopleTrav is "N">>

<<replace "#textbox-reply">>\Enter your error text here.\
            <</replace>>
<<else>>
<<goto [[results]]>><</if>>
<</button>>
<span id="textbox-reply"></span>
<<button "RESULTS">>

<<if $airTrav is "N" and $carTrav is "N" and $trainTrav is "N" and $minerals is "N" and $electricity is "N" and $shipping is "N" and $nonlocalFood is "N" and $peopleTrav is "N">>

<<replace "#textbox-reply">>\Enter your error text here.\
            <</replace>>
<<else>>
<<goto [[results]]>><</if>>
<</button>>
<span id="textbox-reply"></span>

In the next “results” passage, I can put:

<<if $air isnot "N">><<set $resources.push("air travel")>><</if>>
<<if $car isnot "N">><<set $resources.push("car travel")>><</if>>
<<if $train isnot "N">><<set $resources.push("train travel")>><</if>>

<<print $resources>>
<<if $air isnot "N">><<set $resources.push("air travel")>><</if>>
<<if $car isnot "N">><<set $resources.push("car travel")>><</if>>
<<if $train isnot "N">><<set $resources.push("train travel")>><</if>>

<<print $resources>>

Each of the “if” lines adds an item to the $resources array if the checkbox is checked. The result, if “air travel” and “train travel” are checked, is this display:

air travel, train travel

Now, if I want to be able to print these out as a grammatically useful sentence in the text, I put in:

<<set $sentence = $resources.slice(0, -2).join(', ') + ($resources.slice(0, -2).length ? ', ' : '') + $resources.slice(-2).join(' and ');>>

$sentence

<<set $sentence = $resources.slice(0, -2).join(', ') + ($resources.slice(0, -2).length ? ', ' : '') + $resources.slice(-2).join(' and ');>>

$sentence

$sentence will now display “air travel”, “air travel and train travel”, or “air travel, car travel and train travel”, depending on how many boxes are checked.

FOR AN OXFORD COMMA:

<<if $resources.length == 2>>

<<set $sentence = $resources.slice(0, -2).join(', ') + ($resources.slice(0, -2).length ? ', ' : '') + $resources.slice(-2).join(' and ');>>

<<elseif $resources.length gt 2>><<set $sentence = $resources.slice(0, -2).join(', ') + ($resources.slice(0, -2).length ? ', ' : '') + $resources.slice(-2).join(', and ');>>

<<else>><<set $sentence = $resources>>
<</if>>
<<if $resources.length == 2>>

<<set $sentence = $resources.slice(0, -2).join(', ') + ($resources.slice(0, -2).length ? ', ' : '') + $resources.slice(-2).join(' and ');>>

<<elseif $resources.length gt 2>><<set $sentence = $resources.slice(0, -2).join(', ') + ($resources.slice(0, -2).length ? ', ' : '') + $resources.slice(-2).join(', and ');>>

<<else>><<set $sentence = $resources>>
<</if>>