Have an idea?

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

ListMax() blocks the function of my Java script

Dear all,

I've got the following problem: I preparing a questionnaire in which participants are asked if they know certain products out of a pre-defined selection with a multiple select type question.
The products chosen by the participants are then add to a list and this list is randomized and shortened for subsequent questions:
 AIC(ListOfProducts)
Randomize()
ListMax(5)
SortByLabel()

Afterwards it is asked how much the participants would pay for the product including two numeric fields (for "one-time payment" and "rate payment") and one select field (for an "I don't know" opt out).
To ensure that participants do not leave all fields blank or select illogical combinations (e.g. indicating  a price and still select "I don't know") I prepared the following Java Script:
var x = document.getElementById("F5CompetitorPrices_r1_c3").checked;

if(SSI_GetValue("F5CompetitorPrices_r1_c1") > 0 && x == true)
{
    strErrorMessage = "Please insert numeric values or select \I do not know\".";
}

var x = document.getElementById("F5CompetitorPrices_r1_c3").checked;

if(SSI_GetValue("F5CompetitorPrices_r1_c2") > 0 && x == true)
{
    strErrorMessage = "Please insert numeric values or select \I do not know\".";
}

var x = document.getElementById("F5CompetitorPrices_r1_c3").checked;

if(SSI_GetValue("F5CompetitorPrices_r1_c1") == 0 && SSI_GetValue("F5CompetitorPrices_r1_c2") == 0 && x == false)
{
    strErrorMessage = "Please insert numeric values or select \I do not know\".";
}

I expanded the code for as many rows as I have got on my list of products but it stops working after I included the code for a sixth row.

I know that my ListMax(5) is the reason for the Java Script to stop working for more than 5 rows. If I strike out the ListMax-Function the Java Script works perfectly.
However, I need to shorten (and randomize) the list of products chosen in case a participant knows a lot of products - and I do not know how to get my Script to working without stiking the ListMax(5).

I would be very grateful if someone could provide me with a solution for this problem!

Thank you very much in advance and best regards.
asked Aug 13, 2021 by Seb0810 (180 points)

1 Answer

0 votes
What we need to do is get the items of the current respondent's constructed list and have the code only check those rows.  Here are two ways we can do that:

var rows = [% ListValuesArray(list1) %];


var rows = $('input[name="hid_row_list_[% QuestionName() %]"]').val().split(',');


The former is more readable, but I prefer the latter because it doesn't have to be changed if we rename the row list like the other.

Then we can use a loop to go through all rows and apply our logic to each row.  Here's an example based off your verification needs:

var rows = $('input[name="hid_row_list_[% QuestionName() %]"]').val().split(',');
rows.forEach(function(row){
    var c1 = SSI_GetValue('F5CompetitorPrices_r' + row + '_c1');
    var c2 = SSI_GetValue('F5CompetitorPrices_r' + row + '_c2');
    var c3 = SSI_GetValue('F5CompetitorPrices_r' + row + '_c3');
    if (c1 > 0 && c3)
    {
        strErrorMessage = '...';
    }
    if (c2 > 0 && c3)
    {
        strErrorMessage = '...';
    }
    if (c1 == 0 && c2 == 0 && !c3)
    {
        strErrorMessage = '...';
    }
});
answered Aug 13, 2021 by Zachary Platinum Sawtooth Software, Inc. (206,100 points)
Alternatively, we could avoid having to write our own code for this at all if we use "Not Applicable: Per Variable" from the Community Question Library.  It handles adding checkboxes to the ends of rows / columns and requires respondents to either answer the row / column or check the box.  It can be found here:

https://sawtoothsoftware.com/resources/question-library/not-applicable-per-variable
Hello Zachary,

the code works out perfectly well (except one minor change in line 10 - x has to be c3).

I literally just had to copy and paste your/my solution.
Thank you very much!!

Kind regards, Seb
Whoops, looks like I missed changing that variable name in one spot - I've taken care of that now.  Glad I could help.  Hopefully you'll find this forum useful as you use Lighthouse Studio.
Very helpful! Huge argument to use the software.
...