Welcome, Guest
Username: Password: Remember me

TOPIC: Default Answer in Dropdown-Array

Default Answer in Dropdown-Array 1 year 10 months ago #85426

  • ChrWen
  • ChrWen's Avatar
  • OFFLINE
  • Fresh Lemon
  • Posts: 7
  • Karma: 0
Is there any possibility to set the default answer in an array question with dropdown lists?

I've found a javascript for the regular array question with radio buttons that works fine, so I wonder if this can be adapted to a dropdown list?

<script type="text/javascript" charset="utf-8">
 
	$(document).ready(function(){
 
		var qID = QQ;
 
		// Loop through the rows and check the first radio if none are already checked
		$('#question'+qID+' table.question tbody tr').each(function(i) {
			if($('input.radio:checked', this).length == 0) {
				$('input.radio:eq(0)', this).attr('checked', true);
			}
		}); 
	});
 
</script>

If it's important, I use LimeSurvey 1.92+ Build 120711 and a slightly modified Version of the citronade template.

Thanks in advance
The administrator has disabled public write access.

Re: Default Answer in Dropdown-Array 1 year 10 months ago #85553

  • tpartner
  • tpartner's Avatar
  • OFFLINE
  • LimeSurvey Team
  • Posts: 4068
  • Thank you received: 746
  • Karma: 341
ChrWen, if you can attach a small sample survey I'll see if I can put together a little 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: Default Answer in Dropdown-Array 1 year 10 months ago #85614

  • ChrWen
  • ChrWen's Avatar
  • OFFLINE
  • Fresh Lemon
  • Posts: 7
  • Karma: 0
Hi tpartner,

thanks for your help. I've made a small example with only one question. The question is mandatory, so for convenience reasons it would be nice if the dropdown-boxes would show "Nichts" as default (instead of "Please choose..").

Somewhere here at the forum it was recommended to simply rename the "Please choose.."- answer in the translation file (originally the survey is in german), but I use some more dropdown-boxes, where this option "Nichts" makes no sense.

However, thanks for your offer to help.

File Attachment:

File Name: LS_default-answer_33433.lss
File Size: 26 KB



Regards
Christian
The administrator has disabled public write access.

Re: Default Answer in Dropdown-Array 1 year 10 months ago #85625

  • tpartner
  • tpartner's Avatar
  • OFFLINE
  • LimeSurvey Team
  • Posts: 4068
  • Thank you received: 746
  • Karma: 341
1) Set up your survey to use JavaScript.

2) Add the following script to the source of the question. Replace "QQ" with the question ID.

The script loops through all of the drop-downs and, if nothing has been previously selected, the first option that has a value is selected.
<script type="text/javascript" charset="utf-8">
 
	$(document).ready(function(){
 
		$('#questionQQ select').each(function(i) {
			if($(this).val() == '') {
				$('option[value!=""]:first', this).attr('selected', 'selected');
			}
		});
	});
 
</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: 1 year 10 months ago by tpartner.
The administrator has disabled public write access.
The following user(s) said Thank You: ChrWen

Re: Default Answer in Dropdown-Array 1 year 10 months ago #86048

  • ChrWen
  • ChrWen's Avatar
  • OFFLINE
  • Fresh Lemon
  • Posts: 7
  • Karma: 0
Hey Tony,

first, sorry for my late response, but there was a lot of work todo the last week.

I've just tried your code and it works perfect. Thank You, i really appreciate your help.

Bye
Christian
The administrator has disabled public write access.

Default Answer in Dropdown-Array 6 months 2 weeks ago #103507

  • LloydW
  • LloydW's Avatar
  • OFFLINE
  • Gold Donor
  • Posts: 32
  • Thank you received: 1
  • Karma: 0
It's odd but I can't get the code below to work in 2.00.

I have a check box array, and I need people to answer every question (as there is conditional logic that follows). If I make it non mandatory I get the 'no answer' check but setting the 'minimum questions' = total number, it doesn't accept the 'no answer' as a response.

So if I make it mandatory but include a 6th option ('I don't know') how can I make Limesurvey complete that box by default when the question opens? That way if the respondent doesn't change it they have still 'answered' all the questions for the system.

Probably doing something silly. I presume I just copy this code into the question (I know java code is working in my survey through a test one I ran).Question attached.

Ideas would be very welcome.

Lloyd.



<script type="text/javascript" charset="utf-8">
 
	$(document).ready(function(){
 
		var qID = QQ;
 
		// Loop through the rows and check the first radio if none are already checked
		$('#question'+qID+' table.question tbody tr').each(function(i) {
			if($('input.radio:checked', this).length == 0) {
				$('input.radio:eq(0)', this).attr('checked', true);
			}
		}); 
	});
 
</script>
Attachments:
The administrator has disabled public write access.

Default Answer in Dropdown-Array 6 months 2 weeks ago #103515

  • tpartner
  • tpartner's Avatar
  • OFFLINE
  • LimeSurvey Team
  • Posts: 4068
  • Thank you received: 746
  • Karma: 341
Hi Lloyd,

I assume from your sample that you want the first column ("Not Relevant") checked by default.

This will work in LimeSurvey 2.0:
<script type="text/javascript" charset="utf-8">	
 
	$(document).ready(function() {
 
		// Identify this question ID
		var qID = {QID};
 
		// Loop through the rows and check the first radio if none are already checked
		$('#question'+qID+' table.question tbody tr').each(function(i) {
			if($('input.radio:checked', this).length == 0) {
				$('input.radio:eq(0)', this).attr('checked', true);
			}
		}); 
	});
</script>

Here is a working survey with the script in the question source.

File Attachment:

File Name: limesurvey_survey_877746.lss
File Size: 22 KB
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.

Default Answer in Dropdown-Array 6 months 2 weeks ago #103528

  • LloydW
  • LloydW's Avatar
  • OFFLINE
  • Gold Donor
  • Posts: 32
  • Thank you received: 1
  • Karma: 0
Thanks Tony
It was the last column ("Don't know") so this code does correctly check the first column, but when I change the index - eq(6) - it all goes blank.
What am I doing wrong?

Regards, Lloyd.
The administrator has disabled public write access.

Default Answer in Dropdown-Array 6 months 2 weeks ago #103532

  • tpartner
  • tpartner's Avatar
  • OFFLINE
  • LimeSurvey Team
  • Posts: 4068
  • Thank you received: 746
  • Karma: 341
Err...I don't see a "Don't know" column:
Capture2_2014-01-15.PNG


Regardless, if you want the last option in the row to be default, change this:
$('input.radio:eq(0)', this).attr('checked', true);

To this:
$('input.radio:last', this).attr('checked', true);

A note on the selector index - the index stars at 0, so if you are trying to manipulate answer column 6, you would use :eq(5)

.
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.
The following user(s) said Thank You: LloydW

Default Answer in Dropdown-Array 6 months 2 weeks ago #103535

  • LloydW
  • LloydW's Avatar
  • OFFLINE
  • Gold Donor
  • Posts: 32
  • Thank you received: 1
  • Karma: 0
Oh my - I attached the wrong question. :blush:
Thanks though for guiding me through. I can't believe I didn't pick up re the index starting at 0! :ohmy:

Wonderful help. :cheer:
Last Edit: 6 months 2 weeks ago by LloydW. Reason: smilies
The administrator has disabled public write access.
Moderators: ITEd
Time to create page: 0.144 seconds
Donation Image