Welcome to the LimeSurvey Community Forum

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

Default option for array by column

  • Matiasko
  • Matiasko's Avatar Topic Author
  • Offline
  • Junior Member
  • Junior Member
More
6 years 2 weeks ago #165161 by Matiasko
Default option for array by column was created by Matiasko
Hello :)

I would like to know how to make first option (Not applicable) in each column prefilled (default). I already know how to do it in standard array (by row) with the code below:
Code:
<script type="text/javascript" charset="utf-8">
  $(document).on('ready pjax:scriptcomplete',function(){
    // Identify the questions
    var qID = {QID};
    var q = $('#question'+qID);
 
    // Default answers
    $('table.subquestion-list tr.answers-list', q).each(function(i) {
      if($('input[type="radio"]:checked', this).length == 0) {
        $('input[type="radio"]:first', this).trigger('click');
      }
    });
    });
</script>
In addition, I would like to have radio button for first column (I) option 'Not applicable' disabled all the time (no default answer for this column).

My version of Lime: 3.3.1, survey attached.

Thank you in advance

P.S.
It would be helpful to have a page with 'Make default option for question type: xxx' with code for all questions that don't support 'Default answer' or to have it implemented inside LimeSurvey :)
The following user(s) said Thank You: szylkrecik
The topic has been locked.
  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
6 years 2 weeks ago #165164 by tpartner
Replied by tpartner on topic Default option for array by column
This script should meet both of your requirements:

Code:
<script type="text/javascript" charset="utf-8">
  $(document).on('ready pjax:scriptcomplete',function(){
    // Identify this question
    var thisQuestion = $('#question{QID}');
 
    // Loop through first-row answers
    $('tr.answers-list:eq(0) input[type="radio"]', thisQuestion).each(function(i) {
 
      // Disable the first radio in the first column
      if(i == 0) {
        $(this).prop('disabled', true);
      }
      // Default answers
      else {
        var thisName = $(this).attr('name');
        if($('input[type="radio"][name="'+thisName+'"]:checked', thisQuestion).length == 0) {
          $(this).trigger('click');
        }
      }
    });
  });
</script>

Sample survey attached:

File Attachment:

File Name: limesurvey...3521.lss
File Size:19 KB

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: szylkrecik
The topic has been locked.
  • Matiasko
  • Matiasko's Avatar Topic Author
  • Offline
  • Junior Member
  • Junior Member
More
6 years 2 weeks ago #165299 by Matiasko
Replied by Matiasko on topic Default option for array by column
As always works great, thank you!
The topic has been locked.

Lime-years ahead

Online-surveys for every purse and purpose