Welcome, Guest
Username: Password: Remember me

TOPIC: Numeric answer calculations on the same page, then compare to a 'cut-off' number

Numeric answer calculations on the same page, then compare to a 'cut-off' number 3 years 4 months ago #63739

  • doodsonj
  • doodsonj's Avatar
  • OFFLINE
  • Junior Lime
  • Posts: 30
  • Thank you received: 2
  • Karma: 2
Hi Team,

Is it possible to add up all the values in several numeric text fields, and then determine whether this is equal to a certain number (i.e. do the values that the user has inputted in the numeric text answer fields for Q1, Q2, Q3, Q4 add up to 10?).

I've seen the workaround for reusing values from previous questions, though this relies on the values being on a previous page (I have already implemented this solution). However, I'd like to do all the calculation on the same page as the answers rather than on a separate, later page.

If anybody could send me in the right direction or tell me if it is possible then that'd be great.

Thanks J.
Last Edit: 3 years 4 months ago by doodsonj. Reason: spelling
The administrator has disabled public write access.

Re: Numeric answer calculations on the same page, then compare to a 'cut-off' number 3 years 4 months ago #63748

  • tpartner
  • tpartner's Avatar
  • OFFLINE
  • LimeSurvey Team
  • Posts: 4358
  • Thank you received: 826
  • Karma: 381
When do you want to make the calculation? On submit, on text field change or some other trigger?
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: Numeric answer calculations on the same page, then compare to a 'cut-off' number 3 years 4 months ago #63751

  • doodsonj
  • doodsonj's Avatar
  • OFFLINE
  • Junior Lime
  • Posts: 30
  • Thank you received: 2
  • Karma: 2
Hi tpartner,

Thanks for getting back to me :)

Preferably the event would occur on a textfield.onChange event, but it would be useful to know the syntax for the onSubmit (or if this is documented elsewhere).

J.
The administrator has disabled public write access.

Re: Numeric answer calculations on the same page, then compare to a 'cut-off' number 3 years 4 months ago #63754

  • tpartner
  • tpartner's Avatar
  • OFFLINE
  • LimeSurvey Team
  • Posts: 4358
  • Thank you received: 826
  • Karma: 381
To calculate the total of all numeric and multi-numeric inputs when any are changed:
<script type="text/javascript" charset="utf-8">
 
	$(document).ready(function() {
		// Listener on all numeric and multi-numeric inputs
		$('.numeric-multi input.text, .numeric input.text').change(function(){
			var inputTotal = 0;
			$('.numeric-multi input.text, .numeric input.text').each(function(){
				inputTotal = inputTotal + Number($(this).val());
			});
			alert (inputTotal);
		});
 
	});
 
</script>

To calculate the total of all numeric and multi-numeric inputs when next/submit is clicked:
<script type="text/javascript" charset="utf-8">
 
	$(document).ready(function() {
		// Interrupt the Next/Submit function
		$('form#limesurvey').submit(function(){	
 
			// Override the built-in "disable navigation buttons" feature
			$('#moveprevbtn, #movenextbtn, #movesubmitbtn').attr('disabled', '');
 
			var inputTotal = 0;
			$('.numeric-multi input.text, .numeric input.text').each(function(){
				inputTotal = inputTotal + Number($(this).val());
			});
			alert (inputTotal);
			return 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 4 months ago by tpartner.
The administrator has disabled public write access.
The following user(s) said Thank You: doodsonj

Re: Numeric answer calculations on the same page, then compare to a 'cut-off' number 3 years 4 months ago #63756

  • doodsonj
  • doodsonj's Avatar
  • OFFLINE
  • Junior Lime
  • Posts: 30
  • Thank you received: 2
  • Karma: 2
Excellent, I have plugged this into my code and everything is now being done as required. As ever and again... thanks very much Tony :)

J.
The administrator has disabled public write access.
Moderators: ITEd
Time to create page: 0.122 seconds
Donation Image