Have an idea?

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

How to set value in 2nd numeric box on 1st numeric box answer

Hi guys,

Hope you are fine and doing well,

i have a grid question in 1st column i am asking about household items using Checkbox in 2nd column i am asking about no of items using numeric box and in 3rd column score using numeric box...

if 1st item selected and no of item is 1 then score should be 3,
if 1st item selected and no of item is 2 then score should be 5
if 1st item selected and no of item is 4 then score should be 7
till
row 18

                 Items                             no of Items                   Score
Row 1     Item1                                       1                                   3
Row 1     Item1                                       2                                   5
Row 1     Item1                                       4                                   7

Row 2     Item2                                       3                                   5
Row 2     Item2                                       4                                   7
Row 2     Item2                                       2                                   2
.
.
Row 18    Item18                                    2                                  5
Row 18    Item18                                    1                                  3
Row 18    Item18                                    3                                  9

how can i achieve this using loop...
currently i m using below mention code...


<script>
/*For Row 1
var scr1 = [3,5,6,6,6];
$('#[% QuestionName() %]_r1_c2').keyup(function(){
    var ttl1 = 0;
    for (var i = 1; i <= 5; i++) {
        if (SSI_GetValue('[% QuestionName() %]_r1_c2') >= i) {ttl1 = scr1[i - 1];}}
            $('#[% QuestionName() %]_r1_c3').val(ttl1);
});
</script>

<script>
/*For Row 2
var scr2 = [0,2,3,4,5];
$('#[% QuestionName() %]_r2_c2').keyup(function(){
    var ttl2 = 0;
    for (var i = 1; i <= 5; i++) {
        if (SSI_GetValue('[% QuestionName() %]_r2_c2') >= i) {ttl2 = scr2[i - 1];}}
            $('#[% QuestionName() %]_r2_c3').val(ttl2);
});
</script>



Thanks i advance
asked Jun 27 by Nouman Saeed Bronze (995 points)
edited Jun 27 by Nouman Saeed
Is there some pattern on how the scores work across the rows, or does every row just have a totally unique score?
scores are all different...

1 Answer

0 votes
Without a nice score pattern, there's only so much that can be done to shorten and optimize this script.  I would probably start by removing that for loop again - just set the "ttl1" variable with SSI_GetValue.  Then maybe you can remove the multiple keyup events with a single one and a switch statement.

<script>
$('#[% QuestionName() %]_div input').keyup(function(){
    var match = $(this).attr('id').match(/^[% QuestionName() %]_r([0-9]+)_c2$/);
    if (match) {
        switch (match[1]) {
            case 1:
                doTheThing('[% QuestionName() %]', 1, [3, 5, 6, 6, 6]);
                break;
            case 2:
                doTheThing('[% QuestionName() %]', 2, [0, 2, 3, 4, 5]);
                break;
        }
    }
})

function doTheThing(questionName, row, scores) {
    var resp = SSI_GetValue(questionName + '_r' + row + '_c2');
    $('#' + questionName + '_r' + row + '_c3').val(scores[resp - 1]);
}
</script>
answered Jun 28 by Zachary Platinum Sawtooth Software, Inc. (124,225 points)
...