how to fill the answer from the token

Plus d'informations
il y a 3 ans 11 mois #101805 par luker
luker a créé le sujet : how to fill the answer from the token
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.

Connexion pour participer à la conversation.

Plus d'informations
il y a 3 ans 11 mois #101831 par luker
luker a répondu au sujet : 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?

Connexion pour participer à la conversation.

Plus d'informations
il y a 3 ans 11 mois #101834 par tpartner
tpartner a répondu au sujet : 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:

<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.

Connexion pour participer à la conversation.

Plus d'informations
il y a 3 ans 11 mois #101844 par luker
luker a répondu au sujet : 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

Connexion pour participer à la conversation.

Plus d'informations
il y a 3 ans 11 mois #101849 par tpartner
tpartner a répondu au sujet : how to fill the answer from the token
Yes, there should be a space before .input but no dot.

Try this:
<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.

Connexion pour participer à la conversation.

Plus d'informations
il y a 3 ans 11 mois #101863 par luker
luker a répondu au sujet : 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!

Connexion pour participer à la conversation.

Plus d'informations
il y a 3 ans 11 mois - il y a 3 ans 11 mois #101867 par tpartner
tpartner a répondu au sujet : 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:
<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.
Dernière édition: il y a 3 ans 11 mois par tpartner.

Connexion pour participer à la conversation.

Plus d'informations
il y a 3 ans 11 mois #101869 par luker
luker a répondu au sujet : 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 :)

Connexion pour participer à la conversation.

Plus d'informations
il y a 3 ans 11 mois - il y a 3 ans 11 mois #101870 par tpartner
tpartner a répondu au sujet : how to fill the answer from the token
In that case, you should be able to safely use the .click() method:
<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.
Dernière édition: il y a 3 ans 11 mois par tpartner.

Connexion pour participer à la conversation.

Plus d'informations
il y a 3 ans 11 mois #101871 par tpartner
tpartner a répondu au sujet : 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:
<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.

Connexion pour participer à la conversation.

Plus d'informations
il y a 3 ans 10 mois #101920 par luker
luker a répondu au sujet : 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.

Connexion pour participer à la conversation.

Plus d'informations
il y a 3 ans 10 mois #101925 par tpartner
tpartner a répondu au sujet : 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.

Connexion pour participer à la conversation.

Commencez dès maintenant !

Créez simplement un compte et commencez à utiliser LimeSurvey dès aujourd'hui.

Inscrivez-vous maintenant