Have an idea?

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

Is there a syntaxically correct way to insert a javascript variable in SSI script?

Hi,

I'm running some javascript/jQuery/SSIscript in the footer of many questions and, at some point in the code,  to save some time and avoid potential error, I'm trying to do this :
var qList = 'q4aColListA'; // will be different at each and every question
        
        var sec1 = '[% LISTHASPARENTMEMBER(' + qList + ', 1) %]';
        var sec2 = '[% LISTHASPARENTMEMBER(' + qList + ', 2) %]';
        var sec3 = '[% LISTHASPARENTMEMBER(' + qList + ', 3) %]';
        var sec4 = '[% LISTHASPARENTMEMBER(' + qList + ', 4) %]';
        var sec5 = '[% LISTHASPARENTMEMBER(' + qList + ', 5) %]';
        
        var list1 = '[% LISTLENGTH(' + qList + ') = 1 %]';
        var list2 = '[% LISTLENGTH(' + qList + ') = 2 %]';
        var list3 = '[% LISTLENGTH(' + qList + ') = 3 %]';
        var list4 = '[% LISTLENGTH(' + qList + ') = 4 %]';
        var list5 = '[% LISTLENGTH(' + qList + ') = 5 %]';


but when I try to preview the question it says there's a mismatched parenthesis in my code... maybe it's just not possible to insert a variable inside square brackets delimiting SSI script... or maybe there's an other workaround for this?

So is there a way to do what I'm trying to do ?

I thank you so much in advance!

sebas
asked Sep 11, 2015 by sebas (235 points)

2 Answers

0 votes
When using SSI Script within Perl, you shouldn't use the [% and %] brackets.
answered Sep 11, 2015 by Bryan Orme Platinum Sawtooth Software, Inc. (152,255 points)
Thank you Bryan for your answer.

but the code shown above is actually a little chunk of javascript/jquery with a bit of embedded SSI script for I need to retrieve some information from a few constructed lists in order to make some html manipulation...

var sec1 = '[% LISTHASPARENTMEMBER(q4aColListA, 1) %]';
        var sec2 = '[% LISTHASPARENTMEMBER(q4aColListA, 2) %]';
        var sec3 = '[% LISTHASPARENTMEMBER(q4aColListA, 3) %]';
        var sec4 = '[% LISTHASPARENTMEMBER(q4aColListA, 4) %]';
        var sec5 = '[% LISTHASPARENTMEMBER(q4aColListA, 5) %]';
        
        var list1 = '[% LISTLENGTH(q4aColListA) = 1 %]';
        var list2 = '[% LISTLENGTH(q4aColListA) = 2 %]';
        var list3 = '[% LISTLENGTH(q4aColListA) = 3 %]';
        var list4 = '[% LISTLENGTH(q4aColListA) = 4 %]';
        var list5 = '[% LISTLENGTH(q4aColListA) = 5 %]';
        
        var c11 = '<td id="cycle1">1<sup>er</sup>CYCLE</td>';
        var c12 = '<td id="cycle1" colspan="2">1<sup>er</sup>CYCLE</td>';
        var c21 = '<td id="cycle2" colspan="1">2<sup>e</sup>CYCLE</td>';
        var c22 = '<td id="cycle2" colspan="2">2<sup>e</sup>CYCLE</td>';
        var c23 = '<td id="cycle2" colspan="3">2<sup>e</sup>CYCLE</td>';
        
        
        if (list5)
            {
                $('.column_header_row').before('<tr><td class="corner_label_cell" width="50%">&nbsp;</td><td id="cycle1" colspan="2">1<sup>er</sup>CYCLE</td><td id="cycle2" colspan="3">2<sup>e</sup>CYCLE</td></tr>');
            }
        else if (list1) {
                if ((sec1 == 1) || (sec2 == 1)) {
                    $('.column_header_row').before('<tr><td class="corner_label_cell" width="50%">&nbsp;</td>' + c11 + '</tr>');
                    console.log('c11')
                } else {
                        $('.column_header_row').before('<tr><td class="corner_label_cell" width="50%">&nbsp;</td>' + c21 + '</tr>');
                        console.log('c21')
                    }
        } 
and so on....

The variables were created to determine which and where a colspan value should be assigned to the two td cells to be added as a header according to some previous answers...

My apologies for not being very clear on what my question was...

Many thanks again Bryan (for the try ;) !

sebas
Sorry, jumped to conclusions.  Let's wait for somebody who knows about use of SSI Script commands within javascript to respond.
0 votes
Not my strongest area, but I don't believe this will work due to a kind of "order of operations" effect.

SSI Script and custom Perl is server-side code (think skip logic, constructed list building, etc.).  It is going to be evaluated when the scripts are figuring out what question to show next to the respondents and building the page.  JavaScript is evaluated by the browser, after the page is built and displayed to the respondent.  So the JavaScript variable doesn't exist when the Perl is evaluated.

The other thing to mention is just because you used the word preview.  Preview just throws up the HTML for a question, but the engine isn't actually running under the hood, so JavaScript and SSI Script won't run unless you are actually running the survey in Test mode in your browser.
answered Sep 14, 2015 by Brian McEwan Gold Sawtooth Software, Inc. (38,765 points)
...