Check out the LimeSurvey source code on GitHub!

Change answer to a question

More
5 years 8 months ago - 5 years 8 months ago #60672 by madopr
What I need to do is this:
If a question is answered, I need to hide the next question; but give a value to the answer of that second question depending of the answer of the first one. Is there a way to do this?
Last Edit: 5 years 8 months ago by madopr.

Please Log in to join the conversation.

More
5 years 8 months ago #60686 by tpartner
You can do both of those with JavaScript. There are lots of examples in the forum - search for .hide() and .val(.

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.

Please Log in to join the conversation.

More
5 years 8 months ago #60865 by madopr
Ok, my first question is a numeric input. It's ID is 85348X27X184. My second question is a list (radio). The id of the second question is 85348X27X185 and the codes for each answer are 0, 1, 2, 3 and 4.
I inserted this code in the next section of my survey; but It doesn´t change the answer of my second question. Why?
<script>
$(document).ready(function() {
    if ({INSERTANS:85348X27X184} > 0)
    {
	if ({INSERTANS:85348X27X184} < 16)
	    {
		$('#answer85348X27X185').val(0);
	    }
	else if ({INSERTANS:85348X27X184} < 30)
	    {
		$('#answer85348X27X185').val(1);
	    }
	else if ({INSERTANS:85348X27X184} < 45)
	    {
		$('#answer85348X27X185').val(2);
	    }
	else if ({INSERTANS:85348X27X184} < 65)
	    {
		$('#answer85348X27X185').val(3);
	    }
	else if ({INSERTANS:85348X27X184} >= 65)
	    {
		$('#answer85348X27X185').val(4);
	    }
    }
});
</script>

Please Log in to join the conversation.

More
5 years 8 months ago - 5 years 8 months ago #60882 by tpartner
You can't set a radio question that way, you need to set a "checked" attribute to true. I also think you're missing some quotes.

Assuming your questions are on different pages, try this:
<script type="text/javascript" charset="utf-8">
 
	$(document).ready(function() {
		if ('{INSERTANS:85348X27X184}' > 0)
		{
			if ('{INSERTANS:85348X27X184}' < 16)
			{
				$('#question184 input.radio[value="0"]').attr('checked', true);
			}
			else if ('{INSERTANS:85348X27X184}' < 30)
			{
				$('#question184 input.radio[value="1"]').attr('checked', true);
			}
			else if ('{INSERTANS:85348X27X184}' < 45)
			{
				$('#question184 input.radio[value="2"]').attr('checked', true);
			}
			else if ('{INSERTANS:85348X27X184}' < 65)
			{
				$('#question184 input.radio[value="3"]').attr('checked', true);
			}
			else if ('{INSERTANS:85348X27X184}' >= 65)
			{
				$('#question184 input.radio[value="4"]').attr('checked', true);
			}
		}
	});
 
</script>

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
Last Edit: 5 years 8 months ago by tpartner.

Please Log in to join the conversation.

More
5 years 8 months ago #60885 by madopr
Both questions 184 and 185 are in section 27; and I'm inserting this code in the first question of section 28. I use the alerts to check that the code of the correct option is reached; and it is; but the answer recorded for question 185 doesn't change.
<script>
$(document).ready(function() {
    if ({INSERTANS:85348X27X184} > 0)
    {
	if ({INSERTANS:85348X27X184} < 16)
	    {
		$('#question185 input.radio[value="0"]').attr('checked', true);
		alert("0");
	    }
	else if ({INSERTANS:85348X27X184} < 30)
	    {
		$('#question185 input.radio[value="1"]').attr('checked', true);
		alert("1");
	    }
	else if ({INSERTANS:85348X27X184} < 45)
	    {
		$('#question185 input.radio[value="2"]').attr('checked', true);
		alert("2");
	    }
	else if ({INSERTANS:85348X27X184} < 65)
	    {
		$('#question185 input.radio[value="3"]').attr('checked', true);
		alert("3");
	    }
	else if ({INSERTANS:85348X27X184} >= 65)
	    {
		$('#question185 input.radio[value="4"]').attr('checked', true);
		alert("4");
	    }
    }
});
</script>

Please Log in to join the conversation.

More
5 years 8 months ago #60886 by tpartner
You can't use JavaScript to affect question on previous pages.

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.

Please Log in to join the conversation.

Imprint                   Privacy policy         General Terms & Conditions         Revocation information and revocation form