Welcome to the LimeSurvey Community Forum

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

Set default answer in a Dropdown-Array question

  • Dannyfaithful
  • Dannyfaithful's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
3 years 11 months ago - 3 years 11 months ago #216863 by Dannyfaithful
 

File Attachment:

File Name: LS_default...3433.lss
File Size:26 KB

Hi, I want to set all of the subquestions' default answer to be "0%" in an array question with dropdown lists.I've tried to use the following javascript, but only the first subquestion's default answer has changed to "0%", all the others has remained "please choose".  Can someone help me please? Many thanks! I have attached my question below. 
Code:
</script><script type="text/javascript" charset="utf-8">
 
    $(document).ready(function(){
 
        $('#question{QID}').each(function(i) {
            if($(this).val() == '') {
                $('option[value!=""]:first', this).attr('selected', 'selected');
            }
        });
    });
 
</script>
Last edit: 3 years 11 months ago by Dannyfaithful.
The topic has been locked.
  • Dannyfaithful
  • Dannyfaithful's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
3 years 11 months ago #216876 by Dannyfaithful
Replied by Dannyfaithful on topic Set default answer in a Dropdown-Array question
Photo of the question attached. 
Attachments:
The topic has been locked.
  • Joffm
  • Joffm's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
3 years 11 months ago #216881 by Joffm
Hi,
the easy way:
use an equation before your question to set the default values.
Like
{Q03_SQ001='A1'}
{Q03_SQ002='A1'}
{Q03_SQ003='A11'}  // Here you said, the default value is 100%
{Q03_SQ004='A1'}

 

And in my opinion it is better to code the answer options according the persentages.
Not
"A1" = 0%
"A2" = 10%
...
"A11" = 100%
Very difficult to calculate something.

But
0 = 0%
10 = 10%
...
100 = 100%

Joffm

Volunteers are not paid.
Not because they are worthless, but because they are priceless
The following user(s) said Thank You: Dannyfaithful
The topic has been locked.
  • Dannyfaithful
  • Dannyfaithful's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
3 years 11 months ago #216882 by Dannyfaithful
Replied by Dannyfaithful on topic Set default answer in a Dropdown-Array question
Hi Joffm, thank you for your prompt reply. I saw you are an active member and have already helped many people on the forum. Great to learn from you.

I am still very new to LimeSurvey, so I don't know how to set an equation before the question. Do you mind attaching your modified question/survey in the post. I will try to study your setting on my end.

Another question is - If I want to make sure the sum of the percentage must equal 100%, how can I set up the validation?

Many thanks for your help in advance!!
The topic has been locked.
  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
3 years 11 months ago #216885 by tpartner
Joffm, do you not need an IF() statement to check if the question is already answered?

Regarding the JavaScript, this will set all dropdowns except the third to the first value and set the third dropdown to 100. Note that the answer codes are changed to numerics as Joffm suggests.  
 
 
Code:
<script type="text/javascript" data-author="Tony Partner">
 
    $(document).on('ready pjax:scriptcomplete',function(){
 
        // Default 0 for all but SQ003
    $('#question{QID} select.form-control').not('[id$=SQ003]').each(function(i) {
            if($(this).val() == '') {
        $(this).val($('option[value!=""]:first', this).attr('value')).trigger('change');
            }
        });
 
    // SQ003 defaults to 100%
    if($('#question{QID} select.form-control[id$=SQ003]').val() == '') {
      $('#question{QID} select.form-control[id$=SQ003]').val('100').trigger('change');
    }
    });
 
</script>

Regarding the question validation, try this (after the answer codes are changed to numerics):  

Code:
sum(self) == 100

Sample survey attached: 

File Attachment:

File Name: limesurvey...3(1).lss
File Size:23 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.
  • Joffm
  • Joffm's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
3 years 11 months ago - 3 years 11 months ago #216886 by Joffm
@tpartner, Yes I need one.
In my example it is included.

 

File Attachment:

File Name: limesurvey...3978.lss
File Size:24 KB


With validation (the same Tony showed)

Joffm

By the way:
Why don't you use a question of type "multiple numerical input"? Only to have some drop-downs?
Here all your requirements are built-in.
There are "default answers",
 
there isthe validation
 

 

If you want to remove the "Remaining" and "Total" display, I did it with this css in the question text.
Code:
<style>
.ls-group-remaining, .ls-group-total {
  display:none;
}
</style>

Volunteers are not paid.
Not because they are worthless, but because they are priceless
Last edit: 3 years 11 months ago by Joffm.
The following user(s) said Thank You: DenisChenu
The topic has been locked.
  • Dannyfaithful
  • Dannyfaithful's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
3 years 11 months ago #217080 by Dannyfaithful
Replied by Dannyfaithful on topic Set default answer in a Dropdown-Array question
Hi Tony, thank you for your help. Your code is working very well in my survey. Really appreciated.
The topic has been locked.
  • Dannyfaithful
  • Dannyfaithful's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
3 years 11 months ago #217081 by Dannyfaithful
Replied by Dannyfaithful on topic Set default answer in a Dropdown-Array question
Hi Joffm, thank you for your help. Your suggestion is great. Really appreciated.
The topic has been locked.
Moderators: tpartnerholch

Lime-years ahead

Online-surveys for every purse and purpose