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

TOPIC: Javascript Listener help

Javascript Listener help 3 years 7 months ago #55812

  • paulfiner
  • paulfiner's Avatar
  • OFFLINE
  • Expert Lime
  • Posts: 92
  • Thank you received: 1
  • Karma: 1
I need to screen people out of my survey if they don't select the first option from a multiple choice checkbox question.

I would like to populate a hidden radiobutton question with either 'Yes' or 'No' depending on whether option 1 was checked or not.

I'm fairly sure this can be done by applying a listener to the first checkbox but don't know how to script this. Can anyone help?

I presume I can then set a quota of 0 on the hidden question's 'No' answer?

Cheers

Paul
The administrator has disabled public write access.

Re: Javascript Listener help 3 years 7 months ago #55816

  • Mazi
  • Mazi's Avatar
  • OFFLINE
  • LimeSurvey Team
  • Posts: 5325
  • Thank you received: 296
  • Karma: 249
Hi Paul,

according to the docs you can simply apply a quota directly to the multiple choice question.

Best regards/Beste Grüße,
Dr. Marcel Minke
(Limesurvey Head of Support)
Need Help? We offer professional Limesurvey support
Contact: marcel.minke(at)limesurvey.org'"
The administrator has disabled public write access.

Re: Javascript Listener help 3 years 7 months ago #55821

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

You can't set a quota on people who don't select an option as far as I'm aware.
I need to re-direct these people straight away to a Panel provider if they don't select this option.

I should be able to do it by automatically filling the hidden radio button question though.

Hopefully, tpartner will be along shortly :)

Cheers

Paul
The administrator has disabled public write access.

Re: Javascript Listener help 3 years 7 months ago #55827

  • tpartner
  • tpartner's Avatar
  • OFFLINE
  • LimeSurvey Team
  • Posts: 4188
  • Thank you received: 786
  • Karma: 359
This shouldn't be a problem Paul but I'm tied up today (that pesky work thing!) - lemme play with it tonight.
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: Javascript Listener help 3 years 7 months ago #55830

  • paulfiner
  • paulfiner's Avatar
  • OFFLINE
  • Expert Lime
  • Posts: 92
  • Thank you received: 1
  • Karma: 1
Thanks Tony!
The administrator has disabled public write access.

Re: Javascript Listener help 3 years 7 months ago #55849

  • Mazi
  • Mazi's Avatar
  • OFFLINE
  • LimeSurvey Team
  • Posts: 5325
  • Thank you received: 296
  • Karma: 249
Sorry, my mistake. I overread the "NOT" part.

Maybe you could just change the meaning of question/answer text so that they are filtered if they click it.
Like instead of asking "What about these brands...:"
1. I KNOW about BMW
2. I DON'T know Ford
3. I DON'T know Toyota
...

Of course this is the not so nice but easy solution. Tony might solve that much better.

Best regards/Beste Grüße,
Dr. Marcel Minke
(Limesurvey Head of Support)
Need Help? We offer professional Limesurvey support
Contact: marcel.minke(at)limesurvey.org'"
The administrator has disabled public write access.

Re: Javascript Listener help 3 years 7 months ago #55885

  • tpartner
  • tpartner's Avatar
  • OFFLINE
  • LimeSurvey Team
  • Posts: 4188
  • Thank you received: 786
  • Karma: 359
Okay, here we go.

Add your radio question (do NOT use the "Always hide this question" setting) and then add the following script to the source of the radio question text or help. Replace "MM" (line 6) with the ID of your multi-choice question and "HH" (line 7) with the radio ID.

The script basically does what you described above - hides the hidden question and then interrupts the Next/Submit function and toggles the hidden radio depending on the state of the first checkbox.
<script type="text/javascript" charset="utf-8">
 
	$(document).ready(function() {
 
		// The question IDs
		var qMultiID = MM;
		var qHiddenID = HH;
 
		// Hide the hidden question
		$('#question'+hiddenID+'').hide();
 
		// Interrupt next/submit and toggle the hidden question depending on the state of the first checkbox
		$('form#limesurvey').submit(function(){
 
			if ($('#question'+qMultiID+' input.checkbox:eq(0)').attr('checked') == true) {
				$('#question'+qHiddenID+' input.radio:eq(0)').attr('checked', true);
			}
			else {
				$('#question'+qHiddenID+' input.radio:eq(1)').attr('checked', true);
			}
			return true;
		});
 
 
	});
 
</script>

If you want to test it without hiding the question, comment out this line:
$('#question'+hiddenID+'').hide();

Like so:
//$('#question'+hiddenID+'').hide();
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: Javascript Listener help 3 years 7 months ago #55903

  • paulfiner
  • paulfiner's Avatar
  • OFFLINE
  • Expert Lime
  • Posts: 92
  • Thank you received: 1
  • Karma: 1
Just tried this and everything works!

Thanks Tony, much appreciated. I'm going to try and stick this in a loop as I actually want to screen out respondents if they tick any of the first five codes.

Once I get this survey out of the way I will try and write up a detailed article for the wiki about working with panel providers which incorporates these techniques.

Cheers

Paul
The administrator has disabled public write access.

Re: Javascript Listener help 3 years 7 months ago #55931

  • tpartner
  • tpartner's Avatar
  • OFFLINE
  • LimeSurvey Team
  • Posts: 4188
  • Thank you received: 786
  • Karma: 359
No problem, let me know if you want help looping it.
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: Javascript Listener help 3 years 7 months ago #55932

  • paulfiner
  • paulfiner's Avatar
  • OFFLINE
  • Expert Lime
  • Posts: 92
  • Thank you received: 1
  • Karma: 1
I managed to sort out the looping with a little help from google :)

This is a great method for setting quotas and could also be beneficial in helping to solve/simplify complex conditions with lots of Ands and Ors.

Cheers

Paul
Last Edit: 3 years 7 months ago by paulfiner.
The administrator has disabled public write access.
  • Page:
  • 1
  • 2
Moderators: ITEd
Time to create page: 0.129 seconds
Donation Image