Welcome to the LimeSurvey Community Forum

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

how to fill the answer from the token

More
10 years 4 months ago #101805 by luker
Hi,

I would like to pre-fill some answers based on the data in tokens i.e.
case1:
if TOKEN:ATTRIBUTE_1 = Y then one of "multiple choice" box should be selected
case2:
if TOKEN:ATTRIBUTE_2 = abc then "short text" should be filled by abc

I have found in manual the chapter "Caution about using Assignment Operator (=)" in Expression manager part, but there is no syntax here.
So could please you explain how to use it because my some blind test were unsuccessful.

Thanks in advance for help.
The topic has been locked.
More
10 years 4 months ago #101831 by luker
Replied by luker on topic how to fill the answer from the token
After another manual investigation I can answer myself partially for case2.
For text question I can use "defaults" icon and specify that default should be loaded from the token eg. {TOKEN:ATTRIBUTE_5}. It is fine, default is visible and can be edited.

Problem is still with case1 because "multiple choice" (defaults are defined in a different way) or "table" (there is no defaults option).
I have checked "brute-force" way and added to text question {question1_op1=TOKEN:ATTRIBUTE_5} (where TOKEN:ATTRIBUTE_5 is "Y").
But this approach fixed the choice which cannot be changed by the user (btw the selection is not visible but exists) thus such solution is not proper way to solve the problem, I guess.

So, still the question to the experts: how to 'click' the "multiple choice" or "table" question base on the token?
The topic has been locked.
  • tpartner
  • tpartner's Avatar
  • Away
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
10 years 4 months ago #101834 by tpartner
Replied by tpartner on topic how to fill the answer from the token
I don't know whether you can get there with Expression Manager but you might try something like this with JavaScript:

Code:
<script type="text/javascript" charset="utf-8">  
  var attribute_1 = '{TOKEN:ATTRIBUTE_1}';
 
  $(document).ready(function() { 
 
    // The answer code to be checked
    var checkedCode = 'op1';
 
    if(attribute_1 == 'Y') {
      // Check the option      
      $('#question{QID} .input.checkbox[id$="X{QID}'+checkedCode+'"]').attr('checked', 'checked');
    }
  });
</script>


A few caveats:
- This is untested
- This would also check that option if a respondent returns to the page
- In LimeSurvey 2.05, you will need to change "attr('checked', 'checked')" to "prop('checked', true)"

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
The topic has been locked.
More
10 years 4 months ago #101844 by luker
Replied by luker on topic how to fill the answer from the token
Thanks, but it seems it does not work.
Q1: should be the space before .input? (#question{QID} .input)

anyway with and without space it does not work.
Q2: how to test and improve it?

Thanks in advance.


PS. basic javascript alert works fine, I'm using version 2.00+ Build 131107
The topic has been locked.
  • tpartner
  • tpartner's Avatar
  • Away
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
10 years 4 months ago #101849 by tpartner
Replied by tpartner on topic how to fill the answer from the token
Yes, there should be a space before .input but no dot.

Try this:
Code:
<script type="text/javascript" charset="utf-8">  
  var attribute_1 = '{TOKEN:ATTRIBUTE_1}';
 
  $(document).ready(function() { 
 
    // The answer code to be checked
    var checkedCode = 'op1';
 
    if(attribute_1 == 'Y') {
      // Check the option      
      $('#question{QID} input.checkbox[id$="X{QID}'+checkedCode+'"]').attr('checked', 'checked');
    }
  });
</script>

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
The topic has been locked.
More
10 years 4 months ago #101863 by luker
Replied by luker on topic how to fill the answer from the token
Yes, it works! Field is checked, but...
another question is configured that should be visible when the box is checked, but now, box is checked but corresponding question is still hide... quite strange :)

I have found in my old data that some years ago, I have used syntax such:
document.getElementById('answer111X222X333Op1').click();

it seems such syntax can be also used here - it works as I want. Is there any contraindication to use it?

But what about array question? How to click the answer?
In this case I don't have any related questions thus any way should be fine :)

Thanks for the help!
The topic has been locked.
  • tpartner
  • tpartner's Avatar
  • Away
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
10 years 4 months ago - 10 years 4 months ago #101867 by tpartner
Replied by tpartner on topic how to fill the answer from the token
The problem with .click() is that it will uncheck the option if already checked. Safer to use the checkconditions() function to show your conditional question:
Code:
<script type="text/javascript" charset="utf-8">  
  $(document).ready(function() { 
 
    // The answer code to be checked
    var checkedCode = 'op1';
 
    if(attribute_1 == 'Y') {
      var checkElement = $('#question{QID} input.checkbox[id$="X{QID}'+checkedCode+'"]');
 
      // Check the option      
      $(checkElement).attr('checked', 'checked');
 
      // Fire checkconditions()
      checkconditions($(checkElement).attr('value'), $(checkElement).attr('name'), $(checkElement).attr('type'));
    }
  });
</script>


What type of array? Radio? Checkbox?

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
Last edit: 10 years 4 months ago by tpartner.
The topic has been locked.
More
10 years 4 months ago #101869 by luker
Replied by luker on topic how to fill the answer from the token
Many thanks for detailed analysis!

I have asked about radio array. Hope you can help on that as well :)
The topic has been locked.
  • tpartner
  • tpartner's Avatar
  • Away
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
10 years 4 months ago - 10 years 4 months ago #101870 by tpartner
Replied by tpartner on topic how to fill the answer from the token
In that case, you should be able to safely use the .click() method:
Code:
<script type="text/javascript" charset="utf-8">  
  var attribute_1 = '{TOKEN:ATTRIBUTE_1}';
 
  $(document).ready(function() { 
 
    // The x-axis code of the answer to be clicked
    var xAxisCode = 'A2';
    // The y-axis code of the answer to be clicked
    var yAxisCode = 'SQ003';
 
    if(attribute_1 == 'Y') {
      var clickElement = $('#question{QID} input.radio[id$="X{QID}'+xAxisCode+'-'+yAxisCode+'"]');
 
      // Click the option      
      $(clickElement).click();
    }
  });
</script>

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
Last edit: 10 years 4 months ago by tpartner.
The topic has been locked.
  • tpartner
  • tpartner's Avatar
  • Away
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
10 years 4 months ago #101871 by tpartner
Replied by tpartner on topic how to fill the answer from the token
Come to think of it, you can also make sure that this only occurs if the question has not been previously answered:
Code:
<script type="text/javascript" charset="utf-8">  
  var attribute_1 = '{TOKEN:ATTRIBUTE_1}';
 
  $(document).ready(function() { 
 
    // The x-axis code of the answer to be clicked
    var xAxisCode = 'A2';
    // The y-axis code of the answer to be clicked
    var yAxisCode = 'SQ003';
 
    if(attribute_1 == 'Y') {
      var clickElement = $('#question{QID} input.radio[id$="X{QID}'+xAxisCode+'-'+yAxisCode+'"]');
      var clickRow = $(clickElement).closest('.answers-list');
 
      if($('input.radio:checked', clickRow).length == 0) {
        // Check the option      
        $(clickElement).click();
      }
    }
  });
</script>

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
The topic has been locked.
More
10 years 4 months ago #101920 by luker
Replied by luker on topic how to fill the answer from the token
Wow, what a comprehensive answer :) Thanks!
But, don't know why it doesn't work for me...
Do you have any hints how to debug it to check where could be a problem?
At least IE dosn't show any error message.
The topic has been locked.
  • tpartner
  • tpartner's Avatar
  • Away
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
10 years 4 months ago #101925 by tpartner
Replied by tpartner on topic how to fill the answer from the token
Can you activate a test survey with a token that has multiple uses?

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
The topic has been locked.

Lime-years ahead

Online-surveys for every purse and purpose