Welcome to the LimeSurvey Community Forum

Ask the community, share ideas, and connect with other LimeSurvey users!

Default Answer in Dropdown-Array

  • ChrWen
  • ChrWen's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
11 years 6 months ago #85426 by ChrWen
Default Answer in Dropdown-Array was created 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?

Code:
<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 topic has been locked.
  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
11 years 6 months ago #85553 by tpartner
Replied by tpartner on topic Default Answer in Dropdown-Array
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.
The topic has been locked.
  • ChrWen
  • ChrWen's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
11 years 6 months ago #85614 by ChrWen
Replied by ChrWen on topic Default Answer in Dropdown-Array
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
The topic has been locked.
  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
11 years 6 months ago - 11 years 6 months ago #85625 by tpartner
Replied by tpartner on topic Default Answer in Dropdown-Array
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.
Code:
<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: 11 years 6 months ago by tpartner.
The following user(s) said Thank You: ChrWen
The topic has been locked.
  • ChrWen
  • ChrWen's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
11 years 5 months ago #86048 by ChrWen
Replied by ChrWen on topic Default Answer in Dropdown-Array
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 topic has been locked.
More
10 years 2 months ago #103507 by LloydW
Replied by LloydW on topic Default Answer in Dropdown-Array
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.



Code:
<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>
The topic has been locked.
  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
10 years 2 months ago #103515 by tpartner
Replied by tpartner on topic Default Answer in Dropdown-Array
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:
Code:
<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.
The topic has been locked.
More
10 years 2 months ago #103528 by LloydW
Replied by LloydW on topic Default Answer in Dropdown-Array
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 topic has been locked.
  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
10 years 2 months ago #103532 by tpartner
Replied by tpartner on topic Default Answer in Dropdown-Array
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:
Code:
$('input.radio:eq(0)', this).attr('checked', true);

To this:
Code:
$('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.
The following user(s) said Thank You: LloydW
The topic has been locked.
More
10 years 2 months ago - 10 years 2 months ago #103535 by LloydW
Replied by LloydW on topic Default Answer in Dropdown-Array
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: 10 years 2 months ago by LloydW. Reason: smilies
The topic has been locked.
More
6 years 4 months ago #160146 by achecchini
Replied by achecchini on topic Default Answer in Dropdown-Array
This is greatfull!! Have used this wonderful workaround in my survey with success!!
My problem now is: how to change the code for make it functioning in case of use images for radio buttons and checkboxes?? I apply the manual solutions indicate here: manual.limesurvey.org/Workarounds:_Quest...later_implementation
using the suggestion for: LimeSurvey 2.0 and later implementation
I'm studing the imagetick_lime_2.js code without finding solutions ... :-(

Can you help me ??
Thanks in advance ...
A.
The topic has been locked.
  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
6 years 4 months ago #160148 by tpartner
Replied by tpartner on topic Default Answer in Dropdown-Array
You have not explained which workaround you are trying - there are several posted here.

Furthermore, if using a current version of LimeSurvey, I do not recommend the out-dated imageTick plugin. I would use CSS pseudo-elements, something like in this post - www.limesurvey.org/forum/design-issues/1...radio-buttons#149174

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
The topic has been locked.

Lime-years ahead

Online-surveys for every purse and purpose