Have an idea?

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

Other specify in Grid

In grid question I have 5 attributes ask in row, I want to do like like below:

https://drive.google.com/file/d/1grc309bmlli4KKf9rfS-si06FpE1c6rQ/view?usp=sharing

How can I do that? or Do you have any way?
asked Jul 23 by Saroeun Bronze (2,715 points)
If your grid must be set to row-oriented, we will need to use two grid questions to achieve this, one set to use radio buttons with those handful of columns and another set to use open-ends with that one column.  Then you can combine the two grid questions with the code here:

https://legacy.sawtoothsoftware.com/forum/28890/add-text-boxes-to-grid
I have to remember to use this from time to time. Comes in handy.

Thanks as always Zachary. Onya mate.
Glad to hear it.  I considered adding it to the CQL, but it felt somewhat incomplete without validation code and writing generic validation code that would work nicely for every type of grid seemed like a lot.
Saroeun, that code only modifies the HTML to make the two questions appear next to each other.  To apply response requirements between the two questions, you'll need to use custom JavaScript verification.  How do you want to require respondents to answer this combined grid?
Zachary, in each row, If the respondent selects answer 4 they must give an answer in the box. If they answer in the box they must select answer 4. In alert error message, please specify which row error. Thanks

1 Answer

+1 vote
 
Best answer
Set the second grid question to not require a response and give the first grid question this custom JavaScript verification:

var otherGrid = $('#[% QuestionName() %]_div').nextAll('.grid').get(0).id.replace(/_div$/, '');
var rows = $('input[name="hid_row_list_[% QuestionName() %]"]').val().split(',');
for (var r = 0; r < rows.length && !strErrorMessage; r++) {
    var answeredOtherRadio = SSI_GetValue('[% QuestionName() %]_r' + rows[r]) == 4;
    var answeredOtherField = SSI_GetValue(otherGrid + '_r' + rows[r] + '_c1');
    if (answeredOtherRadio && !answeredOtherField) {
        strErrorMessage = 'Other specify field must be answered on row ' + (r + 1) + '.';
    }
    else if (answeredOtherField && !answeredOtherRadio) {
        strErrorMessage = 'Other specify field must be left blank if not selecting other on row ' + (r + 1) + '.';
    }
}
answered Jul 27 by Zachary Platinum Sawtooth Software, Inc. (184,925 points)
selected Jul 29 by Saroeun
Thanks Zachary,

I need your help again. I have a grid question as numeric input box and there are "other specify". Example if they have "Other specify" they need input 1 box for numeric and 1 box for other specify. How can I do that?
Can you expound on your criteria?  There are multiple columns and the last one is meant to be the "other specify" field?
Zachary, please check the link below:

https://drive.google.com/file/d/1u7FGymetzIOv3gExplmXFvi-MTCs4oIZ/view?usp=sharing

Hope you understand.
Try this custom JavaScript verification:

var rows = $('input[name="hid_row_list_[% QuestionName() %]"]').val().split(',');
var columns = $('input[name="hid_col_list_[% QuestionName() %]"]').val().split(',');
for (var c = 0; c < columns.length && !strErrorMessage; c++) {
    var column = columns[c];
    var otherSpecifies = $('[name="[% QuestionName() %]_c' + column + '_other"]');
    if (otherSpecifies.length && otherSpecifies.val().trim()) {
        var answeredAnyRow = false;
        for (var r = 0; r < rows.length && !answeredAnyRow; r++) {
            answeredAnyRow = $('#[% QuestionName() %]_r' + rows[r] + '_c' + column).val().trim();
        }
        if (!answeredAnyRow) {
            strErrorMessage = 'At least one field must be answered if selecting other specify on column ' + (c + 1) + '.';
        }
    }
}
It's not working. I have create 2 questions in the same page like before and add this script to custom JavaScript. I did wrong, right? Can you help to detail?
Sorry for multiple question. Can I use this script with Multiple check box in grid? I have tried but it work only for single radio in grid.

var otherGrid = $('#[% QuestionName() %]_div').nextAll('.grid').get(0).id.replace(/_div$/, '');
var rows = $('input[name="hid_row_list_[% QuestionName() %]"]').val().split(',');
for (var r = 0; r < rows.length && !strErrorMessage; r++) {
    var answeredOtherRadio = SSI_GetValue('[% QuestionName() %]_r' + rows[r]) == 4;
    var answeredOtherField = SSI_GetValue(otherGrid + '_r' + rows[r] + '_c1');
    if (answeredOtherRadio && !answeredOtherField) {
        strErrorMessage = 'Other specify field must be answered on row ' + (r + 1) + '.';
    }
    else if (answeredOtherField && !answeredOtherRadio) {
        strErrorMessage = 'Other specify field must be left blank if not selecting other on row ' + (r + 1) + '.';
    }
}
I need more information about what is not working as intended.
Related to below script, I have created 2 grid questions in the same page. First grid as Numeric and Second grid as Open-End. Then, I input this script into first grid custom JavaScript and not working. Please let see the link for your reference: https://drive.google.com/file/d/12TW0v7Ns_Y6kdPDUUgB8AVawt8kysr-X/view?usp=sharing

var rows = $('input[name="hid_row_list_[% QuestionName() %]"]').val().split(',');
var columns = $('input[name="hid_col_list_[% QuestionName() %]"]').val().split(',');
for (var c = 0; c < columns.length && !strErrorMessage; c++) {
    var column = columns[c];
    var otherSpecifies = $('[name="[% QuestionName() %]_c' + column + '_other"]');
    if (otherSpecifies.length && otherSpecifies.val().trim()) {
        var answeredAnyRow = false;
        for (var r = 0; r < rows.length && !answeredAnyRow; r++) {
            answeredAnyRow = $('#[% QuestionName() %]_r' + rows[r] + '_c' + column).val().trim();
        }
        if (!answeredAnyRow) {
            strErrorMessage = 'At least one field must be answered if selecting other specify on column ' + (c + 1) + '.';
        }
    }
}
That's a totally different picture than the last one you showed demonstrating the grid question you're using.
Yes, sorry my mistake, before I was uploaded the old version. So, this is what I need to do.
...