Welcome, Guest
Username: Password: Remember me

TOPIC: Change answer to a question

Change answer to a question 3 years 3 months ago #60672

  • madopr
  • madopr's Avatar
  • OFFLINE
  • Junior Lime
  • Posts: 26
  • Karma: 0
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: 3 years 3 months ago by madopr.
The administrator has disabled public write access.

Re: Change answer to a question 3 years 3 months ago #60686

  • tpartner
  • tpartner's Avatar
  • OFFLINE
  • LimeSurvey Team
  • Posts: 4117
  • Thank you received: 760
  • Karma: 346
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.

LimeSurvey is open-source and run entirely by volunteers so please consider donating to support the project.
The administrator has disabled public write access.

Re: Change answer to a question 3 years 3 months ago #60865

  • madopr
  • madopr's Avatar
  • OFFLINE
  • Junior Lime
  • Posts: 26
  • Karma: 0
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>
The administrator has disabled public write access.

Re: Change answer to a question 3 years 3 months ago #60882

  • tpartner
  • tpartner's Avatar
  • OFFLINE
  • LimeSurvey Team
  • Posts: 4117
  • Thank you received: 760
  • Karma: 346
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.

LimeSurvey is open-source and run entirely by volunteers so please consider donating to support the project.
Last Edit: 3 years 3 months ago by tpartner.
The administrator has disabled public write access.

Re: Change answer to a question 3 years 3 months ago #60885

  • madopr
  • madopr's Avatar
  • OFFLINE
  • Junior Lime
  • Posts: 26
  • Karma: 0
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>
The administrator has disabled public write access.

Re: Change answer to a question 3 years 3 months ago #60886

  • tpartner
  • tpartner's Avatar
  • OFFLINE
  • LimeSurvey Team
  • Posts: 4117
  • Thank you received: 760
  • Karma: 346
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.

LimeSurvey is open-source and run entirely by volunteers so please consider donating to support the project.
The administrator has disabled public write access.
Moderators: ITEd
Time to create page: 0.140 seconds
Donation Image