Welcome, Guest
Username: Password: Remember me

TOPIC: Slider problem

Slider problem 3 years 1 week ago #63456

  • paulfiner
  • paulfiner's Avatar
  • OFFLINE
  • Expert Lime
  • Posts: 92
  • Thank you received: 1
  • Karma: 1
Hi

I have two sliders on a page (2 separate questions) and I would like to check the value of the second one and only allow respondent's to proceed if the 2nd slider is higher than the first slider.

I've modified some Javascript I found in the forums to populate a hidden multiple numeric question after the 2nd slider but I can't get the script right to do the actual comparison of the 2 values and put up an error message.

Here's my javascript so far:
<script type="text/javascript" charset="utf-8">
 
	$(document).ready(function(){
 
		var qSlider1 = 211;
		var qSlider2 = 232;
		var qHidden = 236;
 
		// Hide the hidden question
		//$('#question'+qHidden).hide();
 
		// Interrupt next/submit function
		$('form#limesurvey').submit(function(){
 
			// Loop through the sliders
			$('#question'+qSlider1+' .multinum-slider').each(function(i) {
 
				// Find the slider value
				var sliderVal1 = $('div[id^="slider-callout"]', this).text().substring(1,3);
 
				// Populate the appropriate hidden question input
				$('#question'+qHidden+' input.text:eq(0)').val(sliderVal1);
 
			});
 
                $('#question'+qSlider2+' .multinum-slider').each(function(i) {
 
				var sliderVal2 = $('div[id^="slider-callout"]', this).text().substring(1,3);
				// Populate the appropriate hidden question input
				$('#question'+qHidden+' input.text:eq(1)').val(sliderVal2);
 
			});
 
			return true;
		});
 
	});
 
</script>
The administrator has disabled public write access.

Re: Slider problem 3 years 1 week ago #63472

  • tpartner
  • tpartner's Avatar
  • OFFLINE
  • LimeSurvey Team
  • Posts: 4062
  • Thank you received: 742
  • Karma: 341
Paul, do you need the hidden question? Would it be good enough to just compare the 2 values and pop up the alert accordingly?

Does each multi-numeric only have a single sub-question? (or can you attach the group here?)
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: Slider problem 3 years 1 week ago #63490

  • paulfiner
  • paulfiner's Avatar
  • OFFLINE
  • Expert Lime
  • Posts: 92
  • Thank you received: 1
  • Karma: 1
Hi Tony

I don't need the hidden question if it can be done without it although I might need access to the values of the sliders on the next page (group).

Each multi-numeric has just one sub-question.

In an ideal world I would like to adjust the minimum value of the 2nd slider 'live' but I'm not sure that's do-able?

Cheers

Paul
The administrator has disabled public write access.

Re: Slider problem 3 years 1 week ago #63515

  • tpartner
  • tpartner's Avatar
  • OFFLINE
  • LimeSurvey Team
  • Posts: 4062
  • Thank you received: 742
  • Karma: 341
You should be able to access the slider values later in the survey with {INSERTANS}.

Dynamically adjusting the slider minimum isn't possible but this should prevent advancing in the survey if slider 2 is not greater than slider 1. Replace "11" and "22" with the slider question IDs.
<script type="text/javascript" charset="utf-8">
 
	$(document).ready(function() {
 
		// Adjust these IDs and message as necessary
		var q1ID = 11;
		var q2ID = 22;
		var msg = 'The second answer must be higher than the first.';
 
		// Interrupt the Next/Submit function
		$('form#limesurvey').submit(function(){	
 
			// Override the built-in "disable navigation buttons" feature
			$('#moveprevbtn, #movenextbtn, #movesubmitbtn').attr('disabled', '');
 
			// Get the slider values
			var slider1 = Number($('#question'+q1ID+' div.slider_callout').text());
			var slider2 = Number($('#question'+q2ID+' div.slider_callout').text());
			if(!slider1) {	
				slider1 = 0;
			}
			if(!slider2) {	
				slider2 = 0;
			}
 
			// Alert and abort submit if slider 2 is not greater than slider 1
			if(slider1 >= slider2) {	
				alert (msg);
				return false;
			}
			else {
				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.
The administrator has disabled public write access.

Re: Slider problem 3 years 1 week ago #63532

  • paulfiner
  • paulfiner's Avatar
  • OFFLINE
  • Expert Lime
  • Posts: 92
  • Thank you received: 1
  • Karma: 1
Hi Tony

Thanks for the answer, I forgot to mention that my sliders have a '£' suffix on the slider callout which stops the javascript from working but after a bit of googling I managed to strip of the suffix from the two slider variables.

Thanks again.

Cheers

Paul
The administrator has disabled public write access.

Re: Slider problem 3 years 1 week ago #63533

  • paulfiner
  • paulfiner's Avatar
  • OFFLINE
  • Expert Lime
  • Posts: 92
  • Thank you received: 1
  • Karma: 1
I forgot to ask....is it possible to add the suffix character to the min and max values displayed at each end of the slider?

Finally, I don't know if this is a bug or not but my values go from 10 to 42 with a step of 2 but you have to move the slider two 'positions' before the value updates which looks a little strange for the user.

Shouldn't it just increment the value by 2 for each movement of the slider?
The administrator has disabled public write access.

Re: Slider problem 3 years 1 week ago #63535

  • tpartner
  • tpartner's Avatar
  • OFFLINE
  • LimeSurvey Team
  • Posts: 4062
  • Thank you received: 742
  • Karma: 341
...is it possible to add the suffix character to the min and max values displayed at each end of the slider?
Add the following line to your script right after the "var msg =" line:
$('.slider_showmin, .slider_showmax').append('£');
Finally, I don't know if this is a bug or not but my values go from 10 to 42 with a step of 2 but you have to move the slider two 'positions' before the value updates which looks a little strange for the user.
Yeah, that may be a bug. It only seems to happen when a min and max value are used.
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 1 week ago by tpartner.
The administrator has disabled public write access.

Re: Slider problem 3 years 1 week ago #63536

  • paulfiner
  • paulfiner's Avatar
  • OFFLINE
  • Expert Lime
  • Posts: 92
  • Thank you received: 1
  • Karma: 1
Hi Tony

Your script puts the £ character after the numeric value. Can it be put before the number?

Cheers

Paul
The administrator has disabled public write access.

Re: Slider problem 3 years 1 week ago #63537

  • tpartner
  • tpartner's Avatar
  • OFFLINE
  • LimeSurvey Team
  • Posts: 4062
  • Thank you received: 742
  • Karma: 341
$('.slider_showmin, .slider_showmax').prepend('£');
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: Slider problem 3 years 1 week ago #63538

  • paulfiner
  • paulfiner's Avatar
  • OFFLINE
  • Expert Lime
  • Posts: 92
  • Thank you received: 1
  • Karma: 1
Magic, thanks!
The administrator has disabled public write access.
Moderators: ITEd
Time to create page: 0.180 seconds
Donation Image