The LimeSurvey Fund-Raiser 2012 is complete. Thank you for donating a total of 25,000 USD!     List of donors »

Welcome, Guest
Username: Password: Remember me
  • Page:
  • 1
  • 2

TOPIC: Sum values different questions

Sum values different questions 2 years 2 months ago #58209

  • steven8000
  • steven8000's Avatar
  • OFFLINE
  • Fresh Lemon
  • Posts: 16
  • Karma: 0
Hi,

I have some numeric fields (different questions) on a page.
If the total equals 100 then they can go to the following page, otherwise they have to change the values.

So I created a javascript:
<script type="text/javascript" charset="utf-8"> 
$(document).ready(function(){ 
	$('form#limesurvey').submit(function(){ 
		if({INSERTANS:7371X1505X1865}+{INSERTANS:7371X1505X1866}+{INSERTANS:7371X1505X1867}==100) {
		return=true;
		}
		else
		{
			return=false;
		}
	)};
)};
</script>
But it doesnt work.
Any ideas what the problem could be?
Where do i have to put this?
In one of the questions? or on the page?
Last Edit: 2 years 2 months ago by steven8000.
The administrator has disabled public write access.

Re: Sum values different questions 2 years 2 months ago #58221

  • tpartner
  • tpartner's Avatar
  • OFFLINE
  • LimeSurvey Team
  • Posts: 2858
  • Thank you received: 424
  • Karma: 244
You cannot use {INSERTANS} for questions on the current page.

You will need to use something like the following, where "11", "22", and "33" are your question IDs.
<script type="text/javascript" charset="utf-8">
 
	$(document).ready(function(){ 
 
		$('form#limesurvey').submit(function(){
 
			var q1ID = 11;
			var q2ID = 22;
			var q2ID = 33;
 
			var q1Answer = $('#question'+q1ID+' input.text').val();
			var q2Answer = $('#question'+q2ID+' input.text').val();
			var q3Answer = $('#question'+q3ID+' input.text').val();
 
			if(Number(q1Answer) + Number(q2Answer) + Number(q3Answer) == 100) {
				return=true;
			}
			else {
				return=false;
			}
		)};
	)};
 
</script>
Cheers,
Tony

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: Sum values different questions 2 years 2 months ago #58224

  • steven8000
  • steven8000's Avatar
  • OFFLINE
  • Fresh Lemon
  • Posts: 16
  • Karma: 0
When I place this code in one of the 3 questions (and change the id's in the script), it doesn't work.

What am i doing wrong?

With regards,

Steven
The administrator has disabled public write access.

Re: Sum values different questions 2 years 2 months ago #58225

  • tpartner
  • tpartner's Avatar
  • OFFLINE
  • LimeSurvey Team
  • Posts: 2858
  • Thank you received: 424
  • Karma: 244
Have you set up your survey to use JavaScript?

Can you get a simple alert to work? Try alerting the value of (q1Answer) + Number(q2Answer) + Number(q3Answer) to make sure it's correct.

Are there any JavaScript errors in the log?
Cheers,
Tony

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: Sum values different questions 2 years 2 months ago #58226

  • steven8000
  • steven8000's Avatar
  • OFFLINE
  • Fresh Lemon
  • Posts: 16
  • Karma: 0
I did it already with alerts.
It look like it doesnt get into the "$(document).ready(function(){" part.
When i do that away and only work with "$('form#limesurvey').submit(function(){", i have the same problem.
An alert alone is no problem.

PS the question types that i use are numeric fields.
Last Edit: 2 years 2 months ago by steven8000.
The administrator has disabled public write access.

Re: Sum values different questions 2 years 2 months ago #58227

  • tpartner
  • tpartner's Avatar
  • OFFLINE
  • LimeSurvey Team
  • Posts: 2858
  • Thank you received: 424
  • Karma: 244
Can you activate a sample survey for me to test?
Cheers,
Tony

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: Sum values different questions 2 years 2 months ago #58229

  • tpartner
  • tpartner's Avatar
  • OFFLINE
  • LimeSurvey Team
  • Posts: 2858
  • Thank you received: 424
  • Karma: 244
I found a couple of syntax errors - try this:
<script type="text/javascript" charset="utf-8">
 
	$(document).ready(function(){ 
 
		$('form#limesurvey').submit(function(){
 
			var q1ID = 11;
			var q2ID = 22;
			var q2ID = 33;
 
			var q1Answer = $('#question'+q1ID+' input.text').val();
			var q2Answer = $('#question'+q2ID+' input.text').val();
			var q3Answer = $('#question'+q3ID+' input.text').val();
 
			if(Number(q1Answer) + Number(q2Answer) + Number(q3Answer) == 100) {
				return true;
			}
			else {
				return false;
			}
		});
	});
 
</script>
Cheers,
Tony

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: Sum values different questions 2 years 2 months ago #58234

  • steven8000
  • steven8000's Avatar
  • OFFLINE
  • Fresh Lemon
  • Posts: 16
  • Karma: 0
I get stuck on "if(Number(q1Answer) + Number(q2Answer) + Number(q3Answer) == 100)"
Do you convert a string to int with "Number()" in javascript?
The administrator has disabled public write access.

Re: Sum values different questions 2 years 2 months ago #58242

  • tpartner
  • tpartner's Avatar
  • OFFLINE
  • LimeSurvey Team
  • Posts: 2858
  • Thank you received: 424
  • Karma: 244
Oops, I'm only half-awake today - found another syntax typo. Try this:
<script type="text/javascript" charset="utf-8">
 
	$(document).ready(function(){ 
 
		$('form#limesurvey').submit(function(){
 
			var q1ID = 11;
			var q2ID = 22;
			var q3ID = 33;
 
			var q1Answer = $('#question'+q1ID+' input.text').val();
			var q2Answer = $('#question'+q2ID+' input.text').val();
			var q3Answer = $('#question'+q3ID+' input.text').val();
 
			if(Number(q1Answer) + Number(q2Answer) + Number(q3Answer) == 100) {
				return true;
			}
			else {
				return false;
			}
		});
	});
 
</script>
Do you convert a string to int with "Number()" in javascript?
Yes.
Cheers,
Tony

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: Sum values different questions 2 years 2 months ago #58257

  • steven8000
  • steven8000's Avatar
  • OFFLINE
  • Fresh Lemon
  • Posts: 16
  • Karma: 0
thanks, this works.

How does it work when you use a "multiple numeric input" question?
The administrator has disabled public write access.
  • Page:
  • 1
  • 2
Moderators: DenisChenu, ITEd
Time to create page: 0.403 seconds
Donation Image