Concating variables

Hi,
I have an action whereby if a user checks a tickbox, then some text is concated on to a variable (using the variable to create a growing list across the presentation). The only issue is that if the user unchecks the tick box, the text remains in the variable and is not removed. Is there a way to remove the text if a user unchecks the button?

Thanks,
Steve

While I do not have time to develop a test project at the moment, I would perhaps attempt to use an array to store each item and remove it from the array if unchecked.

Use a separate function to concatenate the contents of the array after the addition or removal of an item.

1 Like

Hi Steve,

You need to use JavaScript to achieve that.
If it is a question’s checkbox, you can add scripts to the question On Change event > Execute JavaScript action.
If it is independent checkboxes, add scripts to the Execute JavaScript action of each checkbox’s On Check and On Uncheck events.
Here is the general script for both cases:

var checkboxNames = ['Checkbox1', 'Checkbox2'];
var texts = ['Text1', 'Text2'];
var strText = '';
for (var i = 0; i < checkboxNames.length; ++i) {
    if (prez.object(checkboxNames[i]).checked())
        strText += texts[i];
}
prez.variable('variable name', strText);

Regards,

1 Like

Hi, thank you for your quick reply. I have tried this, but am struggling with the uncheck option. Do I replace ‘Test 1’ with nothing? My issue is that when I do that it removes all the text in the variable, not just the text that was added when I clicked on this specific check box. As I am using concate text, my variable has accumulated text after a number of boxes being checked over a number of pages/slides. When I uncheck the box, I only want to delete the text specific to that check box.
Also on your example above you have Checkbox 1 and checkbox 2. I don’t see why I need that second checkbox when my javascript action relates to this specific checkbox, unless that is how you stop it deleting all the text when concating.
Sorry I’ve got a bit confused with this one!

@steve53990 - Here is my attempt at what I understand the question to be.

I have five checkboxes with a value associated. When the user checks the box, that value is added to the list. When the box is unchecked - the value is removed from the list regardless of where it was added. All text is shifted so that gaps do not appear.

There is some JavaScript in the onLoad area as well as part of the check and uncheck actions for each checkbox.

Does this reflect what you are trying to achieve?

concat

@steve53990 - concerning the posted animation of text being added and removed from the display window -

Even if this is not the desired effect you were searching for, I will post my solution to this in the hopes that it will help someone else with something they are hoping to create.

For this - we have some code in three places - one for the onLoad, one for the onCheck, and one for the onUncheck

onLoad

// create the array
prez.variable("notes", []);

// declare the function to remove the unchecked item from display
window.removeItem = function(array, item) {
    for (var i in array) {
        if (array[i]==item) {
            array.splice(i,1);
            break
        }
    }
}

// function to update the display based on
// newly removed or added items.
window.joinItems = function() {
    prez.variable("showNotes", prez.variable("notes").join(" "));
}

onCheck

// add value to the end of the array
prez.variable("notes").push(this.text());

// update the display
joinItems();

onUncheck

// remove value that is unchecked
removeItem(prez.variable("notes"), this.text());

// update the display
joinItems();

Hope this helps!

2 Likes

Hi Gregs, Apologies for the delay in replying. This is exactly what I need. Thank you so much.

2 Likes