Have an idea?

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

Consolidating custom JS verification

I have a three-column grid question, where column 1 is an item, column 2 is my "top," and column 3 is my bottom.

I want to write custom JS that alerts the user if they rated the same item as both top and bottom. I also want to alert the respondent to the specific item(s) that are in violation. Below is the code I have. This works great for one row of the grid, but it's not ideal for many.

How can I adapt this code so I don't have the replicate it for every unique row? Is there a way to recode the [%ListLabel(LIST,1)%] so it smartly IDs the violating row?


(document.mainform['LIST_r1_c1'].checked && document.mainform['LIST_r1_c2'].checked)
    strErrorMessage = "You cannot select [%ListLabel(LIST,1)%] as both \"top\" and \"bottom\".";
asked Mar 22, 2018 by WTW_cal Bronze (1,595 points)

1 Answer

0 votes
Loops are the programming tool for situations like this, where you want to repeat the same code with some minor changes N times.

As for ListLabel, you cannot pass a number from the JavaScript into a Sawtooth Script function.  There are a few ways around this, but the easiest would be to use ListLabelsArray to get an array of all the labels, then in the loop grab the label from the array that we need.

var rowLabels = [% ListLabelsArray(GridQRowList) %];
for (var r = 1; r <= 10; r++) {
    if (document.mainform['GridQ_r' + r + '_c1'].checked && document.mainform['GridQ_r' + r + '_c2'].checked) {
        strErrorMessage = 'You cannot select ' + rowLabels[r - 1] + ' as both "top" and "bottom."';

"GridQ" should be replaced with the name of your grid question, "GridQRowList" with the name of the list of rows, and "10" with the number of rows.

There are still some extra touch ups we could make to this code if you're interested in learning more, but I think this should be sufficient for the current question if that's all you need.
answered Mar 22, 2018 by Zachary Platinum Sawtooth Software, Inc. (183,550 points)