Default Answer in Dropdown-Array

More
4 years 6 months ago #85426 by ChrWen
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

Please Log in to join the conversation.

More
4 years 6 months ago #85553 by tpartner
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.

Please Log in to join the conversation.

More
4 years 6 months ago #85614 by ChrWen
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...3433.lss
File Size:26 KB



Regards
Christian
Attachments:

Please Log in to join the conversation.

More
4 years 6 months ago - 4 years 6 months ago #85625 by tpartner
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.
Last Edit: 4 years 6 months ago by tpartner.
The following user(s) said Thank You: ChrWen

Please Log in to join the conversation.

More
4 years 5 months ago #86048 by ChrWen
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

Please Log in to join the conversation.

More
3 years 2 months ago #103507 by LloydW
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:

Please Log in to join the conversation.

More
3 years 2 months ago #103515 by tpartner
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...7746.lss
File Size:22 KB

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
Attachments:

Please Log in to join the conversation.

More
3 years 2 months ago #103528 by LloydW
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.

Please Log in to join the conversation.

More
3 years 2 months ago #103532 by tpartner
Err...I don't see a "Don't know" column:


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

Please Log in to join the conversation.

More
3 years 2 months ago - 3 years 2 months ago #103535 by LloydW
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: 3 years 2 months ago by LloydW. Reason: smilies

Please Log in to join the conversation.