Have an idea?

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

Skips using Javascript (or Unverified Perl?)

Hi everyone,

I'm working on a survey on Sawtooth SSI Web 8.4.8

I'm asking a series of 15 questions about different traumatic events people have experienced. If they have never experienced a traumatic event I want to skip them directly ahead of this section to another section. The 15 questions are binary numeric variables, with 0 for no and 1 for yes. If the person has experienced one traumatic event I want to skip them ahead to question 1. If they have experienced more than one traumatic event I want to skip them ahead to question 2.

I know how to do this using the built in skipping feature, however for this survey for various reasons I don't want to use it, so I'm trying to achieve the skips using javascript (although if anybody knows a way to do it in Unverified Perl that would be helpful too).

Because there are 15 prior questions and the answers to each of them must be taken into consideration, I was going to try to figure out a way to combine them into an assay and sum the value, so if the total sum of the 15 questions is 0 it will go to the next section, if it is 1 it will go to question 1, and if it is 2 or more it will go to question 2.

I am having trouble figuring out the coding for this however, this is what I'm starting with

setInterval(function () {
    if( SSI_GetValue("trauma") == 1 ) {
        $("#[% QuestionName() %]_div").css('display', 'block');
    }                
    else {
        $("#[% QuestionName() %]_div").css('display', 'none')
    }}, 100);
</script>



and I thought maybe I could use an assay like this

var trauma = ["tram1", "tram2", "tram3", "tram4", "tram5", "tram6", "tram7", "tram8", "tram9", "tram10", "tram11", "tram12", "tram13","tram14", "tram15"]
 


but I need to figure out a way to work it inside the previous coding.

If anybody could help me figure out the coding or give me an alternate method of solving the problem using javacript or Perl that would be great.

Thanks in advance!
asked May 22 by Jake Hammond (215 points)
When you say you want to do this with JavaScript, do you mean you want the follow up questions to dynamically appear / hide on the same page as the questions about trauma?  So while a respondent checks or uncheckes boxes, the two follow up questions automatically respond?
No they don't necessarily have to appear on the same page or automatically respond,  this is just the coding that I was using which would essentially achieve that, but something else would be fine too. As long as I can implement the skips the questions don't need to be on the same page.

1 Answer

+1 vote
 
Best answer
Here's an example of unverified Perl that returns the total of a series of questions:

Begin Unverified Perl
my $total = 0;
my @quests = ('tram1', 'tram2', 'tram3', 'tram4', 'tram5');
foreach my $quest (@quests) {
    $total += GETVALUE($quest);
}
return $total;
End Unverified


If your trauma questions are checkboxes, you'll need to update line 3 to use "tram1_1," "tram2_1," "tram3_1," etc.

To use this for your skip logic, you just need to update line 7 with how you want the skip logic to work.  This will activate with a total of one:

return $total == 1;


This will activate with a total greater than one:

return $total >= 2;
answered May 22 by Zachary Platinum Sawtooth Software, Inc. (63,475 points)
selected May 22 by Jake Hammond
Thanks! When I test the survey both question 1 and question 2 are still coming up however, regardless of how many traumatic experiences selected. Am I putting the code on the correct page? I am putting this in the header for question 1

Begin Unverified Perl
my $total = 0;
my @quests = ('tram1', 'tram2', 'tram3', 'tram4', 'tram5', 'tram6',  'tram7', 'tram8', 'tram9', 'tram10', 'tram11', 'tram12', 'tram13', 'tram14', 'tram15'); foreach my $quest (@quests) {
    $total += GETVALUE($quest);
}
return $total == 1;
End Unverified 


and this on the header for question 2
Begin Unverified Perl
my $total = 0;
my @quests = ('tram1', 'tram2', 'tram3', 'tram4', 'tram5', 'tram6',  'tram7', 'tram8', 'tram9', 'tram10', 'tram11', 'tram12', 'tram13', 'tram14', 'tram15'); foreach my $quest (@quests) {
    $total += GETVALUE($quest);
}
return $total >= 2;
End Unverified 
Those codes can be used as skip logic.  You'll need to use them in skips if you want the other questions to appear on a different page.
Alright, thanks so much!
...