Have an idea?

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

Double Loop in Javascript

hi ,

i have a grid question have 6 column and 10 rows, in which 1st three column have checkbox & other 3 have numeric box...

all numeric boxes are disable for respondent

if 1st column checkbox answered, then enable 1st column numeric box for frequency, if 2nd column checkbox answered then enable 2nd column for frequency and same pattern should be follow in Rows.. i'm doing this through double loop in Javascript but its not working....
please help me to get out of this issue...

Double Loop
 
<script>
$(document).ready(function(){
    SSI_CustomGraphicalCheckbox();
})
 function SSI_CustomGraphicalCheckbox(graphicalCheckboxObj, inputObj) {
 for (var i = 1; i <= 12; i++)
 for (var j = 4; j <= 6; j++)
     {
        if (SSI_GetValue('[% QuestionName() %]_r' + i + '_c' + j) == 1) {
            $('#[% QuestionName() %]_r' + i + '_c' + j).prop('disabled', false);}
        else{
            $('#[% QuestionName() %]_r' + i + '_c' + j).prop('disabled', true);}
    }}
</script>


Same loop is working under single condition....
Single Loop
 
<script>
$(document).ready(function(){
    SSI_CustomGraphicalCheckbox();
})
 function SSI_CustomGraphicalCheckbox(graphicalCheckboxObj, inputObj) {
 for (var i = 1; i <= 12; i++)
     {
        if (SSI_GetValue('[% QuestionName() %]_r' + i + '_c1') == 1) {
            $('#[% QuestionName() %]_r' + i + '_c4').prop('disabled', false);}
        else{
            $('#[% QuestionName() %]_r' + i + '_c4').prop('disabled', true);}
    }}
</script>


Thanks in advance..
asked Oct 10, 2018 by Nouman Saeed Bronze (995 points)
edited Oct 10, 2018 by Nouman Saeed

1 Answer

0 votes
Two things come to mind.

First, you should always surround the contents of every loop with braces.  That means that for two nested loops you need two pairs of braces:

for (var i = 1; i <= 12; i++)
{
    for (var j = 4; j <= 6; j++)
    {
        // ...
    }
}


Second, be mindful of what input elements you are grabbing with regards to the variable "j."  Your checkbox inputs are in columns 1 - 3, but your numeric inputs are in columns 4 - 6.  So what I would probably do is have "j" iterate from 1 to 3, then use "j + 3" when I want to get the numeric elements.  Here's an updated excerpt from your code:

if (SSI_GetValue('[% QuestionName() %]_r' + i + '_c' + j) == 1) {
    $('#[% QuestionName() %]_r' + i + '_c' + (j + 3)).prop('disabled', false);
}
answered Oct 10, 2018 by Zachary Platinum Sawtooth Software, Inc. (126,675 points)
Thank you Zach. but could you please put this code
for (var i = 1; i <= 12; i++)
{
    for (var j = 4; j <= 6; j++)
    {
        // ...
    }
}

into this
<script>
$(document).ready(function(){
    SSI_CustomGraphicalCheckbox();
})
 function SSI_CustomGraphicalCheckbox(graphicalCheckboxObj, inputObj) {
 for (var i = 1; i <= 12; i++)
     {
        if (SSI_GetValue('[% QuestionName() %]_r' + i + '_c1') == 1) {
            $('#[% QuestionName() %]_r' + i + '_c4').prop('disabled', false);}
        else{
            $('#[% QuestionName() %]_r' + i + '_c4').prop('disabled', true);}
    }}
</script>

because i m weak in jquery.. please thanks in advance
You don't need to use the single loop version.  The double loop will work fine.  You just need to add another set of braces: an open brace between lines 6 and 7, and a close brace between lines 13 and 14.  Then replace "j" with "(j - 3)" on line 9.
Hi Zach. i am sending you a CAPI script on your email.
zachary@sawtoothsoftware.com please check...
i have done everything as you mention above but all it vain
Line 9 of your double loop code needs to be replaced with this:

if (SSI_GetValue('[% QuestionName() %]_r' + i + '_c' + (j - 3)) == 1) {
thanks it work fine and sorry for bother you.
...