Skip to main content
Kernzy
Inspiring
December 5, 2022
Answered

Text String from checkbox

  • December 5, 2022
  • 2 replies
  • 800 views

Hey yall,

 

I'm attempting to create an interface that can create a text string from the selections made in a list. It seems rudimentary, but it has me completely stumped. Below you will find what I've pieced together so far, but I'm struggling to string these options together. I'm trying to get the string format to be "Chips and Hot Dog" if those are the two options selected on the list. Any help is appreciated and you may recognize some of your code below! 

function container() {
    var foodTypes = ["Apples","Oranges","Chips","Hot Dogs","Hamburgers","Ice Cream","Pie"]
    // DIALOG
    // ======
    var dialog = new Window("dialog"); 
        dialog.text = "Food Type Selector"; 
        dialog.orientation = "column"; 
        dialog.alignChildren = ["center","top"]; 
        dialog.spacing = 10; 
        dialog.margins = 16; 

    // OPTIONSPANEL
    // ============
    var optionsPanel = dialog.add("panel", undefined, undefined, {name: "optionsPanel"}); 
        optionsPanel.text = "Food Types"; 
        optionsPanel.orientation = "column"; 
        optionsPanel.alignChildren = ["left","top"]; 
        optionsPanel.spacing = 10; 
        optionsPanel.margins = 14;
        optionsPanel.multiselect = true;

    var cbg = optionsPanel.add("group");
        cbg.orientation = "column";
        cbg.alignChildren = ["left","top"]; 

    var patternTypesChecks = [];
    for (var i = 0; i < foodTypes.length; i++) {
        checkBox = cbg.add("checkbox", undefined, foodTypes[i]);
    }

    // DIALOG
    // ======

    var button1 = dialog.add("button", undefined, undefined, {name: "button1"}); 
        button1.text = "Okay"; 
        button1.preferredSize.width = 125;
        button1.onClick = function(){
                var msg = '\r';
                    for (var j=0; j<cbg.children.length; j++){
                        if(cbg.children.value == true){
                            msg += cbg.children.text + '\r';}
                            dialog.close();
        }; };


    dialog.show();
    
    }

container()
alert(msg);

 

This topic has been closed for replies.
Correct answer m1b

Hi @Kernzy@femkeblanco answered just now, but my approach is slightly different so, for learning purposes, have a look at this one too. Both our answers will help you understand I hope. - Mark

function container() {
    var foodTypes = ["Apples", "Oranges", "Chips", "Hot Dogs", "Hamburgers", "Ice Cream", "Pie"]
    // DIALOG
    // ======
    var dialog = new Window("dialog");
    dialog.text = "Food Type Selector";
    dialog.orientation = "column";
    dialog.alignChildren = ["center", "top"];
    dialog.spacing = 10;
    dialog.margins = 16;

    // OPTIONSPANEL
    // ============
    var optionsPanel = dialog.add("panel", undefined, undefined, { name: "optionsPanel" });
    optionsPanel.text = "Food Types";
    optionsPanel.orientation = "column";
    optionsPanel.alignChildren = ["left", "top"];
    optionsPanel.spacing = 10;
    optionsPanel.margins = 14;
    optionsPanel.multiselect = true;

    var cbg = optionsPanel.add("group");
    cbg.orientation = "column";
    cbg.alignChildren = ["left", "top"];

    var patternTypesChecks = [];
    for (var i = 0; i < foodTypes.length; i++) {
        checkBox = cbg.add("checkbox", undefined, foodTypes[i]);
    }

    // DIALOG
    // ======

    var button1 = dialog.add("button", undefined, undefined, { name: "button1" });
    button1.text = "Okay";
    button1.preferredSize.width = 125;
    button1.onClick = function () {
        dialog.close(1);
    };


    var result = dialog.show();

    if (result != 1)
        // user cancelled
        return;

    var values = [];
    for (var j = 0; j < cbg.children.length; j++) {
        if (cbg.children[j].value == true) {
            values.push(cbg.children[j].text);
        }
    };

    return values.join('\r');

};

var msg = container()

if (msg != undefined)
    alert(msg);

2 replies

Kernzy
KernzyAuthor
Inspiring
December 5, 2022

Thank you @femkeblanco  and @m1b!

Both approaches worked perfectly, but Mark's worked better for the situation I'm in.

The good ole' both are right...but one is more right situation. I'm sure I'll find ways to implement both methods at some point! Thanks again!

femkeblanco
Legend
December 5, 2022
var msg = "";
function container() {
    var foodTypes = 
        ["Apples","Oranges","Chips","Hot Dogs","Hamburgers","Ice Cream","Pie"];
    var dialog = new Window("dialog");
        dialog.text = "Food Type Selector";
        dialog.orientation = "column";
        dialog.alignChildren = ["center","top"];
        dialog.spacing = 10;
        dialog.margins = 16;
    var optionsPanel = dialog.add("panel");
        optionsPanel.text = "Food Types";
        optionsPanel.orientation = "column";
        optionsPanel.alignChildren = ["left","top"];
        optionsPanel.spacing = 10;
        optionsPanel.margins = 14;
        optionsPanel.multiselect = true;
    var cbg = optionsPanel.add("group");
        cbg.orientation = "column";
        cbg.alignChildren = ["left","top"];
    var patternTypesChecks = [];
    for (var i = 0; i < foodTypes.length; i++) {
        checkBox = cbg.add("checkbox", undefined, foodTypes[i]);
    }
    var button1 = dialog.add("button"); 
        button1.text = "Okay"; 
        button1.preferredSize.width = 125;
        button1.onClick = function(){
            for (var j=0; j < cbg.children.length; j++) {
                if (cbg.children[j].value == true) {
                    msg += cbg.children[j].text + '\r';
                }
            };
            dialog.close();
        };
    dialog.show();
}
container();
alert(msg);
m1b
Community Expert
m1bCommunity ExpertCorrect answer
Community Expert
December 5, 2022

Hi @Kernzy@femkeblanco answered just now, but my approach is slightly different so, for learning purposes, have a look at this one too. Both our answers will help you understand I hope. - Mark

function container() {
    var foodTypes = ["Apples", "Oranges", "Chips", "Hot Dogs", "Hamburgers", "Ice Cream", "Pie"]
    // DIALOG
    // ======
    var dialog = new Window("dialog");
    dialog.text = "Food Type Selector";
    dialog.orientation = "column";
    dialog.alignChildren = ["center", "top"];
    dialog.spacing = 10;
    dialog.margins = 16;

    // OPTIONSPANEL
    // ============
    var optionsPanel = dialog.add("panel", undefined, undefined, { name: "optionsPanel" });
    optionsPanel.text = "Food Types";
    optionsPanel.orientation = "column";
    optionsPanel.alignChildren = ["left", "top"];
    optionsPanel.spacing = 10;
    optionsPanel.margins = 14;
    optionsPanel.multiselect = true;

    var cbg = optionsPanel.add("group");
    cbg.orientation = "column";
    cbg.alignChildren = ["left", "top"];

    var patternTypesChecks = [];
    for (var i = 0; i < foodTypes.length; i++) {
        checkBox = cbg.add("checkbox", undefined, foodTypes[i]);
    }

    // DIALOG
    // ======

    var button1 = dialog.add("button", undefined, undefined, { name: "button1" });
    button1.text = "Okay";
    button1.preferredSize.width = 125;
    button1.onClick = function () {
        dialog.close(1);
    };


    var result = dialog.show();

    if (result != 1)
        // user cancelled
        return;

    var values = [];
    for (var j = 0; j < cbg.children.length; j++) {
        if (cbg.children[j].value == true) {
            values.push(cbg.children[j].text);
        }
    };

    return values.join('\r');

};

var msg = container()

if (msg != undefined)
    alert(msg);