Setting default values for dropdowns in javascript

More
1 month 3 weeks ago #220829 by ralfkunze
Hello, 

I took a javascript from Toni Partner from this thread forums.limesurvey.org/forum/can-i-do-thi...uestion-type?start=0 and adapted it to my needs (thanks a lot, Toni, for this sript!).
<script type="text/javascript" charset="utf-8">
    
    $(document).on('ready pjax:scriptcomplete',function(){
        var thisQuestion = $('#question{QID}');
 
        // Column-specific classes
        $('tr.subquestion-list', thisQuestion).each(function(i) {
            $('th, td', this).each(function(i) {
                $(this).addClass('column-'+i);
            });
        });
 
        // Insert selects into column 1
        if($('.answer-item.column-1 .inserted-select', thisQuestion).length == 0) {
            $('.answer-item.column-1', thisQuestion).addClass('with-select').append('<select class="inserted-select form-control list-question-select">\
                <option value="">Bitte auswählen...</option>\
                <option value="1">Zunahme um</option>\
                <option value="2" selected>Abnahme um</option>\
            </select>');
        } 
 
        // Listeners on select elements
        $('.inserted-select', thisQuestion).on('change', function(i) {
            if($(this).val() != '') {
                $(this).closest('.answer-item').find('input:text').val($.trim($('option:selected', this).text())).trigger('change');
            }
            else {
                $(this).closest('.answer-item').find('input:text').val('').trigger('change');
            }
        });
 
        // Returning to page
        $('.with-select input:text', thisQuestion).each(function(i) {
            var thisCell = $(this).closest('.answer-item');
            var inputText = $.trim($(this).val());
            var selectval = $('select.inserted-select option', thisCell).filter(function () { return $(this).html() == inputText; }).val();
            $('select.inserted-select', thisCell).val(selectval);
        });
 
        // Listener on column 2 inputs
        $('.answer-item.column-2 input:text', thisQuestion).on('keyup change', function(e) {
            var thisValue = $.trim($(this).val());
            
            // Numerics only
            if($.isNumeric(thisValue) === false) {
                
                // Strip out non-numerics characters
                newValue = thisValue.replace(/\D/g,'');
                $(this).val(newValue).trigger('change');
            }
            
            // Max/min values
            var maxAllowed = 100;
            var minAllowed = 0;
            if (thisValue > maxAllowed) {
                alert('The maximum allowed is '+maxAllowed+'.');
                $(this).val(maxAllowed);
            }
            else if (thisValue < minAllowed) {
                alert('The minimum allowed is '+minAllowed+'.');
                $(this).val(minAllowed);
            }       
        });
        
        // Clean-up styles
        $('select.inserted-select', thisQuestion).css({
            'max-width': '100%'
        });
        $('.with-select input:text', thisQuestion).css({
            'position': 'absolute',
            'left': '-9999em'
        });
    });
</script>
<style type="text/css">.custom-array table.subquestion-list col {
    width: auto !important;
  }
  .custom-array table.subquestion-list thead .column-0 {  width: 20%; }
  .custom-array table.subquestion-list thead .column-1 {  width: 20%; }
</style>

Now I would like to set a default value for the dropdown fields: "Zunahme" should be preselected. Unfortunately I could not find any thread in the forum with a solution to this... 
Thanks in advance, 
Ralf

 

Please Log in to join the conversation.

More
1 month 3 weeks ago - 1 month 3 weeks ago #220834 by Joffm
Hi,
the way without any javascript is to use an equation like

{Q1a_Y001_X002=if(is_empty(Q1a_Y001_X002),2,Q1a_Y001_X002)}
{Q1a_Y002_X002=if(is_empty(Q1a_Y002_X002),2,Q1a_Y002_X002)}
...


 

BTW: Good point. I will add it to my tutorial about arrays.

Joffm


Volunteers are not paid.
Not because they are worthless, but because they are priceless
Attachments:
Last edit: 1 month 3 weeks ago by Joffm.
The following user(s) said Thank You: tpartner, ralfkunze

Please Log in to join the conversation.

More
1 month 3 weeks ago #220836 by tpartner
Yes, this is the best solution.

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
1 month 3 weeks ago #220856 by ralfkunze
Hi Joffm,
thanks for the quick reply! Now I am not sure, where I am supposed to insert that equation. As I read in this thread , the equation goes into Logic - Question validation equation. If I put it there, it won't work. Can you help me any further?

Is your tutorial about arrays accessible for everybody?

Thanks a lot,
Ralf

Please Log in to join the conversation.

More
1 month 3 weeks ago #220862 by tpartner
Place it in an equation type question, in the same group, directly preceding the array question.

- manual.limesurvey.org/Question_type_-_Equation

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: ralfkunze

Please Log in to join the conversation.

More
1 month 3 weeks ago #220871 by Joffm
Hi, "ralfkunze"

Is your tutorial about arrays accessible for everybody?

referring to your username I think you speak German.
Have a look in the German part of the forum.

Joffm


Volunteers are not paid.
Not because they are worthless, but because they are priceless
The following user(s) said Thank You: ralfkunze

Please Log in to join the conversation.

More
1 month 3 weeks ago #220921 by ralfkunze
Hi,
I tried everything but I cannot get it to work. I checked the manual about equation questions, I downloaded tutorial for arrays from Joffm (thanks a lot for this excellent work!) and set up a whole new question... but it did not work out.
So I saved my questions in a file an attach it to this message. Can you please look over it? 
Ralf
Attachments:

Please Log in to join the conversation.

More
1 month 3 weeks ago #220930 by Joffm
Hi,
well, use the script of the tutorial and everything is fine.
I did this and removed the part of different column widths, You obviously did not use it.

 

File Attachment:

File Name: limesurvey...9467.lss
File Size:345 KB


Joffm

BTW: Your question is only an example, not a serious question, isn't it.
In welchem Maße verzeichneten Sie im Jahr 2021 Rückgänge 
combined with the preset of "decrease by"

If you ask the question this way, you do not need the array, a "multiple numeric question" is sufficient.
A better question text to matvh this type and the answer options:
In welchem Maße verzeichneten Sie im Jahr 2021 Rückgänge oder Zuwächse 
.
 


Volunteers are not paid.
Not because they are worthless, but because they are priceless
Attachments:
The following user(s) said Thank You: ralfkunze

Please Log in to join the conversation.

More
1 month 2 weeks ago #221016 by ralfkunze
Sorry, but my answer yesterday somehow wasn't posted... thank you for the corrections of the script and the wording! Yes - it was the first raw version.

Whats the advantage of using arrays over multiple numeric question? From my point of view right now they are both quite elaborate.
Ralf

Please Log in to join the conversation.

Start now!

Just create your account and start using Limesurvey today.

Register now