Have an idea?

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

How to display the first selected from a multiple response

Hello,

I have a list called "BRAND" and it has 15 members in it.  How do I show the first item selected in the next question?  Currently I have [%ListLabel (BRAND, 1) %] but it is showing the first item "shown" and selected in the list... I want the item the respondent SELECTED first...

I'm using SSI Web version 7.

Please let me know if you can help!
asked Jun 4, 2018 by anonymous

1 Answer

+1 vote
Add a free format question to the same page as your checkbox select question.  The free format needs a hidden, text variable named "_order."  Give the free format this HTML:

<input name="[% QuestionName() %]_order" id="[% QuestionName() %]_order" type="hidden" value=""/>

<script>
function SSI_CustomGraphicalCheckbox(graphicalObj, inputObj, checked) {
    var orderText = document.querySelectorAll('#[% QuestionName() %]_order')[0].value;
    var order = [];
    if (orderText) {
        order = orderText.split(',').map(Number);
    }
    
    var item = -1;
    var match = inputObj.id.match(/_([0-9]*)$/);
    if (match) {
        item = Number(match[1]);
    }
    
    if (checked) {
        order.push(item);
    }
    else {
        var index = order.indexOf(item);
        order.splice(index, 1);
    }
    
    orderText = order.join(',');
    document.querySelectorAll('#[% QuestionName() %]_order')[0].value = orderText;
}
</script>


Now create a constructed list whose parent list is the list used by the checkbox select question.  Give this constructed list this code:

Begin Unverified Perl
my $plist = 'list1';
my $freeFormat = 'ff';

my $orderText = GETVALUE($freeFormat . '_order');
if ($orderText) {
    my @order = split(',', $orderText);
    foreach my $item (@order) {
        ADD($plist, $item);
    }
}
End Unverified


Line 2 should be updated with the name of the parent list.  Line 3 should be updated with the name of the free format question.

Now you have a constructed list of the items selected by the respondent, in order of selection.  You can use the Sawtooth Script function ListLabel to print out the first item selected.
answered Jun 4, 2018 by Zachary Platinum Sawtooth Software, Inc. (111,375 points)
Question on Free
...