Welcome to the LimeSurvey Community Forum

Ask the community, share ideas, and connect with other LimeSurvey users!

Test questions / question groups based on token attributes

  • michaelve
  • michaelve's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
9 years 1 month ago - 9 years 1 month ago #117144 by michaelve
In several questions we use the token attributes of the respondent in the question's javascript. For instance:

--javascript being passed to php
var tokenAttribute1 = {TOKEN:ATTRIBUTE_1};

--php script
$sql = "select * from foo WHERE attr='".$tokenAttribute1."'" ;

While testing these questions (lay-out etc) however, we would like some default value for the tokenAttribute1 without going through the survey each time we wish to see some changes. As the {TOKEN:ATTRIBUTE_1} is not set or defined when testing a single question, javascript throws a syntax error. I was wondering whether there is solution along the lines of this topic in the bugtracker: where a isset function is proposed. I think the expression manager passes an object or variable back after parsing the attribute, but I do not know how to evaluate this.

Psuedo code would be:

tokenAttribute1 = 0;
if(isset{TOKEN:ATTRIBUTE_1})
{
tokenAttribute1={TOKEN:ATTRIBUTE_1};
}

A similar test could be used in questions relying on answers from previous question - a bit like this topic

Thanks in advance!
Last edit: 9 years 1 month ago by michaelve. Reason: Change in variable name
The topic has been locked.
  • tpartner
  • tpartner's Avatar
  • Away
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
9 years 1 month ago - 9 years 1 month ago #117154 by tpartner
The problem is that Expression Manager wraps the variable in several span elements if it is undefined. These span elements will cause JavaScript errors unless enclosed in quotes.

So try something like this to prevent those errors and detect the span elements:

Code:
<script type="text/javascript" charset="utf-8">  
  $(document).ready(function() {    
    var tokenAttribute1 = "{TOKEN:ATTRIBUTE_1}";
    if(tokenAttribute1.indexOf('<span') >= 0) {
      tokenAttribute1 = 0;
    }
  });
</script>

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
Last edit: 9 years 1 month ago by tpartner.
The following user(s) said Thank You: michaelve
The topic has been locked.
  • michaelve
  • michaelve's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
9 years 1 month ago #117160 by michaelve
Great, thanks, it works! The quotes indeed appear in the generated webpage. Instead of checking for the span, testing for the length works as well.
Code:
var tokenAttribute1 = "{TOKEN:ATTRIBUTE_1}";
    if(tokenAttribute1.length <= 0) {
      tokenAttribute1 = 0;
    } 

Cheers, Michael
The topic has been locked.

Lime-years ahead

Online-surveys for every purse and purpose