Have an idea?

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

Showing specific text in Grid based on responses in the same row

Hello all,

Just wondering if is it possible to show a specific text in a grid row based on respondent input on the same row. For example, during a time-use survey, in a grid that lists daily activities a row is a specific activity conducted by the respondent. If the respondent inputs start time and duration, is it possible to show an additional column that calculated the end time of the activity based on this input? I'm pretty sure this would require some js..

Thank you!
Giannis
asked Mar 18 by Giannis Tsouros
This could be done.  How are the users inputting their start time and duration?  Are these dropdowns?  Textboxes where people enter a date?  Something else?
Thank you. They would be numeric boxes (3 separate for start hour, start minutes, duration)

1 Answer

0 votes
Add a fourth column to your grid question, then give your question this script:

<script>
$(document).ready(function(){
    $('input[name="hid_row_list_[% QuestionName() %]"]').val().split(',').forEach(function(row){
        var hourInput = $('#[% QuestionName() %]_r' + row + '_c1');
        var minuteInput = $('#[% QuestionName() %]_r' + row + '_c2');
        var durationInput = $('#[% QuestionName() %]_r' + row + '_c3');
        var inputs = $(hourInput).add(minuteInput).add(durationInput);
        var endTime = $('#[% QuestionName() %]_r' + row + '_c4');
        $(endTime).prop('disabled', true);
        $(inputs).keyup(function(){
            $(endTime).val('');
            var hour = Number($(hourInput).val().trim() || NaN);
            if (!hour) {
                return;
            }
            var minute = Number($(minuteInput).val().trim() || NaN);
            if (!minute) {
                return;
            }
            var duration = Number($(durationInput).val().trim() || NaN);
            if (!duration) {
                return;
            }
            minute += duration;
            while (minute >= 60) {
                hour++;
                minute -= 60;
            }
            hour = hour % 12;
            $(endTime).val((hour ? hour : 12) + ':' + ('0' + minute).slice(-2));
        });
    });
})
</script>
answered Mar 18 by Zachary Platinum Sawtooth Software, Inc. (144,125 points)
Works flawlessly, many thanks!
...