Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

How do I make a checkbox stay checked?

Hey - I'm super noob. Don't know much of anything about coding and I'm trying to wrap my head around this. Here is some a passage a created with a list of what I called "Qualifiers".

<input type="checkbox"name="check" value="checked"> (print: $Qualifier's Qualifier1)
<input type="checkbox"name="check" value="checked">(print: $Qualifier's Qualifier2)
<input type="checkbox"name="check" value="checked">(print: $Qualifier's Qualifier3)
<input type="checkbox"name="check" value="checked">(print: $Qualifier's Qualifier4)
<input type="checkbox"name="check" value="checked">(print: $Qualifier's Qualifier5)
<input type="checkbox"name="check" value="checked">(print: $Qualifier's Qualifier6)

Is there any easy way to make the checkboxes stay clicked once the use clicks them? Any help is much appreciated. Thank you.

Comments

  • Unlike the SugarCube story format Harlowe does not have built-in support for HTML input fields, so you will need to use Javascript to implement that support.

    To complicate the issue Harlowe does not have a documented Javascript API, so there is no documented way to programatically update the story variable(s) needed to remember which checkboxs were selected by the Reader.

    csalzman has created a hack that uses Javascript code combined with a named hook and a (live:) macro to simulate the functionality needed but unfortunately their code does not support the checkbox.

    To further complicate the issue (based on your example) you want to store the list of Qualifiers selected by the Reader within the same variable (name="check") which means that you would need to process the String value contained within the named hook into an Array within the (live:) macro code which means that the delimiter used within the String needs to be a character that will never be included in the value of each checkbox .

    I don't have the time at the moment to design and write a solution to this, would you consider changing to the SugarCube story format which has a <<checkbox>> macro built-in?
  • Yes I would consider changing the story format if that would make it easier
  • Could you potentially provide an example of what that would look like?
  • Based on your previous two comments I am assuming you want to know how to implemented your request using SugarCube 1.x

    1. Initialise your variables within the StoryInit special passage.

    You will need two arrays, one to story your Qualifiers and the other to store which check-boxes were selected. These two arrays should have the same number of elements.
    <<set $qualifiers to ["one", "two", "three"]>>
    <<set $checks to [false, false, false]>>
    

    2a. Showing the check-boxes within a passage that will be visited only once!

    Note that the checks array element you want the check-boxes's result to be saved into is wrapped within quotes.
    <<checkbox "$checks[0]" false true>> <<print $qualifiers[0]>>
    <<checkbox "$checks[1]" false true>> <<print $qualifiers[1]>>
    <<checkbox "$checks[2]" false true>> <<print $qualifiers[2]>>
    

    2b. Showing the check-boxes within a passage that will be re-visited!

    Because you may want to conditionally pre-check some of the check-boxes based on their previous value you need to use a <<print>> macro to dynamically create each check-box.
    <<print '<<checkbox "$checks[0]" false true ' + ($checks[0] ? "checked" : "") + '>> <<print $qualifiers[0]>>'>>
    <<print '<<checkbox "$checks[1]" false true ' + ($checks[1] ? "checked" : "") + '>> <<print $qualifiers[1]>>'>>
    <<print '<<checkbox "$checks[2]" false true ' + ($checks[2] ? "checked" : "") + '>> <<print $qualifiers[2]>>'>>
    
Sign In or Register to comment.