Checkbox

Discussions about creating CAPI applications to run on Android devices
Post Reply
Jay
Posts: 6
Joined: February 16th, 2024, 11:26 am

Checkbox

Post by Jay »

Hello

Please can you help with what code to use to allow a user select one option from multiple options selected in the previous question without showing the rest of the options which have not been selected.

Thank you.
Gregory Martin
Posts: 1796
Joined: December 5th, 2011, 11:27 pm
Location: Washington, DC

Re: Checkbox

Post by Gregory Martin »

If you search for "ischecked" on the forum, you'll see some posts about dynamically working with checkbox selections. But even better is to look at the Simple CAPI example included with CSPro. The logic for MAIN_LANGUAGE essentially does what you are asking for:
PROC MAIN_LANGUAGE

onfocus

   
// set up the value set using languages that the person speaks; if only one, preselect the language
   
ValueSet string mainLanguageValueSet;

    do numeric ctr = 1 while ctr <= MAIN_LANGUAGE_VS1.length()

        if ischecked(MAIN_LANGUAGE_VS1.codes(ctr), LANGUAGES_SPOKEN) then
           
mainLanguageValueSet.add(MAIN_LANGUAGE_VS1.labels(ctr), MAIN_LANGUAGE_VS1.codes(ctr));
        endif;

    enddo;

    if LANGUAGES_SPOKEN_OTHER <> "" then // add the other language
       
mainLanguageValueSet.add(LANGUAGES_SPOKEN_OTHER, "99");
    endif;

    setvalueset(MAIN_LANGUAGE, mainLanguageValueSet);

    // no need to ask the question if they only listed one language for languages spoken
   
if mainLanguageValueSet.length() = 1 then
       
MAIN_LANGUAGE = mainLanguageValueSet.codes(1);
        setproperty(MAIN_LANGUAGE, "Protected", "Yes");

    else
        setproperty
(MAIN_LANGUAGE, "Protected", "No");

    endif;
Jay
Posts: 6
Joined: February 16th, 2024, 11:26 am

Re: Checkbox

Post by Jay »

Thank you, Gregory. Looks like I didn't frame the question well.

Say if I have these value sets as a checkbox and I select Coke and Fanta as my favorite drinks.

Favorite drinks
1. Coke
2. Fanta
3. Malta

I want a situation that Coke and Fanta will be the only options that'll will show in the next question's value set. Among these I want to be able to select one.

Which of your favorite drinks do you buy the most?
1. Coke
2. Fanta


I also want to be able to loop through all the occurrences and perform the same function for each occurrence.
Thanks
emmayashri
Posts: 6
Joined: April 9th, 2024, 3:45 am
Location: India

Re: Checkbox

Post by emmayashri »

Hey hi, please check the example you can get some idea.
Example.

Code: Select all

<script>
function showOptions() {
    var favoriteDrinks = document.getElementsByName('favorite_drinks');
    var selectedDrinks = [];
    for (var i = 0; i < favoriteDrinks.length; i++) {
        if (favoriteDrinks[i].checked) {
            selectedDrinks.push(favoriteDrinks[i].value);
        }
    }
    var drinksToDisplay = document.getElementsByName('drinks_to_display');
    for (var j = 0; j < drinksToDisplay.length; j++) {
        drinksToDisplay[j].style.display = 'none';
    }
    for (var k = 0; k < selectedDrinks.length; k++) {
        var drink = selectedDrinks[k];
        var element = document.getElementById(drink);
        if (element) {
            element.style.display = 'block';
        }
    }
}
</script>

<form>
    <input type="checkbox" name="favorite_drinks" value="coke" onclick="showOptions()"> Coke<br>
    <input type="checkbox" name="favorite_drinks" value="fanta" onclick="showOptions()"> Fanta<br>
    <input type="checkbox" name="favorite_drinks" value="malta" onclick="showOptions()"> Malta<br>
</form>

<div id="coke" name="drinks_to_display" style="display: none;">
    <label>Which of your favorite drinks do you buy the most?</label><br>
    <input type="radio" name="coke_option" value="Coke"> Coke<br>
    <input type="radio" name="coke_option" value="Fanta"> Fanta<br>
</div>

<div id="fanta" name="drinks_to_display" style="display: none;">
    <label>Which of your favorite drinks do you buy the most?</label><br>
    <input type="radio" name="fanta_option" value="Coke"> Coke<br>
    <input type="radio" name="fanta_option" value="Fanta"> Fanta<br>
</div>

<div id="malta" name="drinks_to_display" style="display: none;">
    <label>Which of your favorite drinks do you buy the most?</label><br>
    <input type="radio" name="malta_option" value="Coke"> Coke<br>
    <input type="radio" name="malta_option" value="Fanta"> Fanta<br>
</div>
Jay
Posts: 6
Joined: February 16th, 2024, 11:26 am

Re: Checkbox

Post by Jay »

Thank you Emmayashri. It works. But please I'd like to know if there's a logic which works in opposite of the ischecked function; one that identifies items that have not been checked in an occurrence.

Thank you for your help.
Gregory Martin
Posts: 1796
Joined: December 5th, 2011, 11:27 pm
Location: Washington, DC

Re: Checkbox

Post by Gregory Martin »

You can use the negation operator:
if not ischecked("A", LANGUAGES_SPOKEN) then
Jay
Posts: 6
Joined: February 16th, 2024, 11:26 am

Re: Checkbox

Post by Jay »

Thank you Gregory
Post Reply