Have an idea?

Visit Sawtooth Software Feedback to share your ideas on how we can improve our products.

How can I create a temporary variable within the branching script

I need to do a poverty question, but it should only be shown under specific conditions based on household size and income.
I tried the code below, but that breaks the branching AND doesn't do my alert. The alert is just for testing to see if the first part works.
I'm guessing I need to put the variable definition somewhere else.

[CODE]
<script>

var householdSize = Q1_r1 + Q1_r2 + Q1_r3 + Q1_rn;

          
var GLOBAL_SAWTOOTH_[% QUESTIONNAME() %]_SETTINGS = {
    branchings: [
                    {
                                               dependentQuestion: 'povertyQuestion',
                        condition: function() {
                            return (householdSize) == 1 &&
                                    SSI_GetValue('Income') == 3);
                        },
                        selectMinimum: 1

    ],
    errorMessageSettings: {
        missingAnswer: 'A response is required.',
        minimumChecks: 'The minimum number of checks is [MINIMUM].',
        minimumCharacters: 'The response must contain at least [MINIMUM] characters.'
    }
};

if (householdSize >= 2) {
      alert (householdSize);
  }
</script>
asked 6 days ago by Nwiggin Bronze (1,310 points)
Where are your Q1_r1, Q1_r2, Q1_r3, and Q1_rn variables being defined and set?  I'm guessing this is a scope issue: the householdSize variable is probably trying to use these Q1-related variables before they exist.

If these variables come from variables of your survey, we could just update the branching's condition to calculate the household size when it needs it with SSI_GetValue or Sawtooth Script.  There's no limit to what code we put into the condition.

condition: function() {
    var householdSize = SSI_GetValue('Q1_r1') + SSI_GetValue('Q1_r2') + SSI_GetValue('Q1_r3') + SSI_GetValue('Q1_rn');
    return householdSize == 1 && SSI_GetValue('Income') == 3;
},


condition: function() {
    var householdSize = [% Q1_r1 %] + [% Q1_r2 %] + [% Q1_r3 %] + [% Q1_rn %];
    return householdSize == 1 && SSI_GetValue('Income') == 3;
},


Is the calculation behind one of these variables more complex than that, though?  If Q1_rn had some big calculation we didn't want to have to repeat again in the branching code, we may be able to record that value in some shared location that the branching code can access.  We'd have to be careful that the value gets set before the branching code runs it.
The Q1_r1, etc. Variables are questions on the same page as the dependentQuestion. They are from a numeric grid and each row represents an age range. For example Q1_r1 is "number of people under 5" Q1_r2, is 'number of people age 5 to 10' and so on.

The dependent question is a poverty question and is based on the people in the household (hence the var householdSize) and the household income. For example, if the householdSize == 3 and Income == 4 then I want to display the povertyQuestion having it ask, "Is your household income above or below $21,960?"
Perhaps your quickest solution then would be to use my SSI_GetValue example, but add a line in the middle to update the text displayed in your follow-up question.  This condition:

condition: function() {
    var householdSize = ...;
    $('.povertySpan').text(householdSize);
    return ...;
},


would update this HTML in the follow-up question:

<span class="povertySpan"></span>


Currently it just puts the household size in the span.  That line of code could be replaced with whatever math is needed to get the value we want to display in the follow-up question.  Will that work?
Ok, so I'm getting over complicated here. hopefully the code below pastes correctly.  I can get the branching to work, but I cannot get the <span> text to work. I have tried an all-at once strategy (first bit of code where nothing works), and I have also tried separating the code into two places which is where the branching works but not the <span>.

First bit-o-code:
I'm am is creating two temporary variables. One called independentQuestion and one called responseOptions.

The independentQuestion variable is set using the conditional statements below it. It is used with responseOptions to try to create the <span> text and I am also using it to try and trigger the branching (which is not working)  

<script>
          
var GLOBAL_SAWTOOTH_[% QUESTIONNAME() %]_SETTINGS = {
    branchings: [
                    { //Making the poverty question appear if required.
                        dependentQuestion: 'HHPoverty',
                        condition: function() {
                            $(document).ready(function(){
                                updateSelectedText();
                            })
   
                        $(document).on('lighthouseRadioButtonChanged', function(event, graphicalObj, inputObj) {
                                updateSelectedText();
                        })
   
                        function updateSelectedText() {
                                var independentQuestion; //this is a temporary variable just created to hold information defined below
                                var responseOptions = [% ListLabelsArray(TempPovertyList) %];    //This is a temporary variable that is assigned to the preconstructed
                                        //The if else if is assigning values to the independentQuestion variable based on household size and income.
                                        if         (SSI_GetValue('QAgeOfHouseholdMembers_r_total_c1') == 1    && SSI_GetValue('HHIncome') == 2) {
                                            independentQuestion = 1;
                                        }
                                        else if (SSI_GetValue('QAgeOfHouseholdMembers_r_total_c1') == 2    && SSI_GetValue('HHIncome') == 3) {
                                            independentQuestion = 2;
                                        }                                
                                        else if (SSI_GetValue('QAgeOfHouseholdMembers_r_total_c1') == 3    && SSI_GetValue('HHIncome') == 4) {
                                            independentQuestion = 3;
                                        }
                                        else if (SSI_GetValue('QAgeOfHouseholdMembers_r_total_c1') == 4    && SSI_GetValue('HHIncome') == 5) {
                                            independentQuestion = 4;
                                        }
                                        else if (SSI_GetValue('QAgeOfHouseholdMembers_r_total_c1') == 5    && SSI_GetValue('HHIncome') == 6) {
                                            independentQuestion = 5;
                                        }
                                        else if (SSI_GetValue('QAgeOfHouseholdMembers_r_total_c1') == 6    && SSI_GetValue('HHIncome') == 6) {
                                            independentQuestion = 6;
                                        }
                                        else if (SSI_GetValue('QAgeOfHouseholdMembers_r_total_c1') == 7    && SSI_GetValue('HHIncome') == 7) {
                                            independentQuestion = 7;
                                        }
                                        else if (SSI_GetValue('QAgeOfHouseholdMembers_r_total_c1') == 8    && SSI_GetValue('HHIncome') == 7) {
                                            independentQuestion = 8;
                                        }                                        
                                $('.mySelectedText').text(responseOptions[SSI_GetValue(independentQuestion)]); // this sets up the text to be displayd. to make it work you need to paste the following where you want the text shown: <span class="mySelectedText"></span>
                        }                            
                            
                            return independentQuestion >= 1;//condition no which to show the poverty question
                        },
                        selectMinimum: 1
                    },

    ],
    errorMessageSettings: {
        missingAnswer: 'A response is required.',
        minimumChecks: 'The minimum number of checks is [MINIMUM].',
        minimumCharacters: 'The response must contain at least [MINIMUM] characters.'
    }
};

</script>



More just on the <span> text. I started out using this block that you helped me write earlier (https://legacy.sawtoothsoftware.com/forum/29587/update-displayed-question-text-on-same-page):

<script>
  
$(document).ready(function(){
    updateSelectedText();
})
   
$(document).on('lighthouseRadioButtonChanged', function(event, graphicalObj, inputObj) {
    updateSelectedText();
})
   
function updateSelectedText() {
    var independentQuestion = 'ReturnToNormalConditions';
    var responseOptions = [% ListLabelsArray(ReturnToNormalDispList) %];
    $('.mySelectedText').text(responseOptions[SSI_GetValue(independentQuestion) - 1]);
}
  
</script>


I then tried to modify it in a similar manner to the branching modification, but the <span> text is not appearing.


<script>

$(document).ready(function(){
    updateSelectedText();
})
   
$(document).on('lighthouseRadioButtonChanged', function(event, graphicalObj, inputObj) {
    updateSelectedText();
})
   
function updateSelectedText() {
    var independentQuestion;
    var responseOptions;
        if         (SSI_GetValue('QAgeOfHouseholdMembers_r_total_c1') == 1    && SSI_GetValue('HHIncome') == 2) {
                    independentQuestion = 1;
        }
        else if (SSI_GetValue('QAgeOfHouseholdMembers_r_total_c1') == 2    && SSI_GetValue('HHIncome') == 3) {
                    independentQuestion = 2;
        }                                
        else if (SSI_GetValue('QAgeOfHouseholdMembers_r_total_c1') == 3    && SSI_GetValue('HHIncome') == 4) {
                    independentQuestion = 3;
        }
        else if (SSI_GetValue('QAgeOfHouseholdMembers_r_total_c1') == 4    && SSI_GetValue('HHIncome') == 5) {
                    independentQuestion = 4;
        }
        else if (SSI_GetValue('QAgeOfHouseholdMembers_r_total_c1') == 5    && SSI_GetValue('HHIncome') == 6) {
                    independentQuestion = 5;
        }
        else if (SSI_GetValue('QAgeOfHouseholdMembers_r_total_c1') == 6    && SSI_GetValue('HHIncome') == 6) {
                    independentQuestion = 6;
        }
        else if (SSI_GetValue('QAgeOfHouseholdMembers_r_total_c1') == 7    && SSI_GetValue('HHIncome') == 7) {
                    independentQuestion = 7;
        }
        else if (SSI_GetValue('QAgeOfHouseholdMembers_r_total_c1') == 8    && SSI_GetValue('HHIncome') == 7) {
                    independentQuestion = 8;
        }                                        
responseOptions = [% ListLabelsArray(TempPovertyList) %];

$('.mySelectedText').text(responseOptions[SSI_GetValue(independentQuestion)]);

}

</script>    
There may be some confusion about the use of events here.  Events allow us to tell JavaScript that when ___ happens, then also do ___.  The document.ready event fires once as part of the process of loading up the web page.  The lighthouseRadioButtonChanged event comes from the BLL and fires whenever the response to a radio button changes.  The thing is: we don't need to manually include either of these in our "condition" function.  Branching already runs this function when the page loads and when a response changes, so we can just put whatever work we want done directly into the function and we'll be all set.

I can help in doing this, but I'm unsure how to interpret this part of your code:

responseOptions[SSI_GetValue(independentQuestion)]


"independentQuestion" can be set to various numbers, but SSI_GetValue expects to be passed the name of a survey variable.  Did you mean to set "independentQuestion" to question names like "Q1," "Q2," "Q3," etc., perhaps?  Or did you mean to remove SSI_GetValue and use your variable as an indexer directly like this?

responseOptions[independentQuestion]
Ok, your response is making sense.  Because both document.ready and lighthouseRadioButtonChanged are fired, I can pull them out of the code (but having them in won't hurt will it?)

Here is a general explanation.  If number of people in household and income are at set parameters we need to ask a follow-up question to determine if they are above/below the poverty line.  So this is broken into two tasks.

Task 1 (which now works reference lines 1 thru 34) is to make the HHPoverty question appear given specific conditions (combination between household size and income).

Task 2 (which does not work but I'm attempting on lines 36-39) is to insert dynamic text into the HHPoverty question.  This text is pulled from a predefined list, called TempPovertyList.  So, I'm using lines 36-39 to try to set up the list and then using
<span class="mySelectedText"></span>

where I want the text to appear.

If I were to guess what's going wrong is I'm running the function updateSelectedText in the wrong place.

{ //Making the poverty question appear if required.
    dependentQuestion: 'HHPoverty',
    condition: function() {
        var independentQuestion; //this is a temporary variable just created to hold information defined below

        //The if else if is assigning values to the independentQuestion variable based on household size and income.
            if         (SSI_GetValue('QAgeOfHouseholdMembers_r_total_c1') == 1    && SSI_GetValue('HHIncome') == 2) {
                        independentQuestion = 1;
                    }
            else if (SSI_GetValue('QAgeOfHouseholdMembers_r_total_c1') == 2    && SSI_GetValue('HHIncome') == 3) {
                        independentQuestion = 2;
                    }                                
            else if (SSI_GetValue('QAgeOfHouseholdMembers_r_total_c1') == 3    && SSI_GetValue('HHIncome') == 4) {
                        independentQuestion = 3;
                    }
            else if (SSI_GetValue('QAgeOfHouseholdMembers_r_total_c1') == 4    && SSI_GetValue('HHIncome') == 5) {
                        independentQuestion = 4;
                    }
            else if (SSI_GetValue('QAgeOfHouseholdMembers_r_total_c1') == 5    && SSI_GetValue('HHIncome') == 6) {
                        independentQuestion = 5;
                    }
            else if (SSI_GetValue('QAgeOfHouseholdMembers_r_total_c1') == 6    && SSI_GetValue('HHIncome') == 6) {
                        independentQuestion = 6;
                    }
            else if (SSI_GetValue('QAgeOfHouseholdMembers_r_total_c1') == 7    && SSI_GetValue('HHIncome') == 7) {
                        independentQuestion = 7;
                    }
            else if (SSI_GetValue('QAgeOfHouseholdMembers_r_total_c1') == 8    && SSI_GetValue('HHIncome') == 7) {
                        independentQuestion = 8;
                    }
            else    {
                        independentQuestion = 0;
                    }                                        
    return independentQuestion >= 1;//condition no nwhich to show the poverty question

    function updateSelectedText() {
        var responseOptions = [% ListLabelsArray(TempPovertyList) %];
        $('.mySelectedText').text(responseOptions[independentQuestion]);
        }
    },
    selectMinimum: 1
},

1 Answer

0 votes
It doesn't look like you're calling the function currently.  It sounds like we could just drop it and run the work inside the normal function:

condition: function() {
    var independentQuestion;

    if (SSI_GetValue('QAgeOfHouseholdMembers_r_total_c1') == 1 && SSI_GetValue('HHIncome') == 2) {
        independentQuestion = 1;
    }
    else if (SSI_GetValue('QAgeOfHouseholdMembers_r_total_c1') == 2 && SSI_GetValue('HHIncome') == 3) {
        independentQuestion = 2;
    }
    else if (SSI_GetValue('QAgeOfHouseholdMembers_r_total_c1') == 3 && SSI_GetValue('HHIncome') == 4) {
        independentQuestion = 3;
    }
    else if (SSI_GetValue('QAgeOfHouseholdMembers_r_total_c1') == 4 && SSI_GetValue('HHIncome') == 5) {
        independentQuestion = 4;
    }
    else if (SSI_GetValue('QAgeOfHouseholdMembers_r_total_c1') == 5 && SSI_GetValue('HHIncome') == 6) {
        independentQuestion = 5;
    }
    else if (SSI_GetValue('QAgeOfHouseholdMembers_r_total_c1') == 6 && SSI_GetValue('HHIncome') == 6) {
        independentQuestion = 6;
    }
    else if (SSI_GetValue('QAgeOfHouseholdMembers_r_total_c1') == 7 && SSI_GetValue('HHIncome') == 7) {
        independentQuestion = 7;
    }
    else if (SSI_GetValue('QAgeOfHouseholdMembers_r_total_c1') == 8 && SSI_GetValue('HHIncome') == 7) {
        independentQuestion = 8;
    }
    else {
        independentQuestion = 0;
    }

    var responseOptions = [% ListLabelsArray(TempPovertyList) %];
    $('.mySelectedText').text(responseOptions[independentQuestion]);

    return independentQuestion >= 1;
}
answered 5 days ago by Zachary Platinum Sawtooth Software, Inc. (171,950 points)
This did it, thank you!
...