Welcome to the LimeSurvey Community Forum

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

Setting a value in a dropdown question with JavaScript

  • hahnimk
  • hahnimk's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
3 years 11 months ago - 3 years 11 months ago #198464 by hahnimk
Dear LimeSurvey community,

I am trying to set the value of a dropdown question with JavaScript.

After searching this forum, I found a function that sets the first non-empty value of the dropdown question, if no 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>

Unfortunately, I don't know how to adapt this code to fit my needs. In my case, I am requesting the value for the dropdown selection with an HTTP request. Afterwards, I simply want to set the value to one of "A1", "A2", "A3", or "A4" (see attached screenshot). My current approach doesn't effect the selected value:
Code:
<script type="text/javascript" charset="utf-8">
// Once document is loaded
$(document).ready(function () {
 
    // HTTP request to get chat bot parameters
    let httpReq = new XMLHttpRequest();
    httpReq.open("GET", "www.URL.com", true);
    httpReq.send();
    httpReq.onreadystatechange = function () {
        if (httpReq.readyState === 4 &amp;&amp; httpReq.status === 200) {
            const response = httpReq.responseText;
            const obj = JSON.parse(response);
            console.log(obj);
 
            const dropDownSelection = "A" + obj.experimentGroup;
 
            $('#question30059 select').each(function(i) {
                if($(this).val() == "") {
                    $('option[value==' + dropDownSelection + ']', this).attr('selected', 'selected');
                }
            });
 
        }
    };
});
</script>

I am looking forward to your responses!


Thanks in advance
Last edit: 3 years 11 months ago by hahnimk.
The topic has been locked.
  • Joffm
  • Joffm's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
3 years 11 months ago #198470 by Joffm
Hi,

first non-empty value of the dropdown question, if no value is selected

What is the "first non-empty value"?

And is it correct that you want to set this value afterwards, if the respondent didn't select anything?

I see in your second script that you want to set the value to "obj.experimentGroup".
How do you get this value?
Is it some kind of a least filled approach? This you can do by an ajax call and ExpressionScript.

So, I'd really appreciate if you explained the design a bit better.


Joffm

BTW: The script you found somewhere is a real old one.
The "QQ" in "#questionQQ" indicates that the question code has to be inserted, like "#question1234".
Nowadays the {QID} is used for that.

Volunteers are not paid.
Not because they are worthless, but because they are priceless
The topic has been locked.
  • hahnimk
  • hahnimk's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
3 years 11 months ago - 3 years 11 months ago #198471 by hahnimk
Hi Joffm,

Thanks for your answer!

You are right, the first script is pretty old (7 years). I got it from the following post from tpartner, where the goal was to set a default value for the dropdown: www.limesurvey.org/de/foren/can-i-do-thi...er-in-dropdown-array .

By the "first non-empty value" I am referring to tpartner's post, where he said that "the first option that has a value is selected". I guess the following code snippet corresponds to this:
Code:
$('option[value!=""]:first', this).attr('selected', 'selected');

In my case, I set up an online experiment, in which participants get invited via email containing a unique user id. Based on this user id, I want to set the pre-determined experiment group (and other variables). So what I did was to set up a server under " www.URL.com " which has an HTTP endpoint that takes the user id as an argument and sends the experiment group back. The experiment group is a number between 1 and 4. The entire workflow is described as follows:
  1. Participant receives user id per email
  2. Participant enters user id in LimeSurvey field
  3. User id is sent via HTTP request to server and experiment group (and other variables) are returned as a response (this is how I get "obj.experimentGroup")
  4. The experiment group (and other variables) are saved in hidden fields of the survey
  5. Based on the experiment group, the participants will see different versions of the survey

I was already able to save the other variables, e.g. the price path, to hidden text fields using this:
Code:
$('#question{30055} input[type="text"]').val([obj.pricePath]);
However, I don't know how to do the corresponding for a dropdown question.

I hope the above clarifies my question.

Thanks a lot
Last edit: 3 years 11 months ago by hahnimk.
The topic has been locked.
  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
3 years 11 months ago #198475 by tpartner

However, I don't know how to do the corresponding for a dropdown question.


Code:
$('#question{QID} select').val(yourVariable);

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: hahnimk
The topic has been locked.
  • hahnimk
  • hahnimk's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
3 years 11 months ago #198476 by hahnimk
Hi tpartner,

Wow, that was easy :laugh:
Works perfectly, thanks a lot!

Can you recommend a documentation to me, where I can get this kind of information from? E.g. how to deal with different question types with JavaScript.

Thanks
The topic has been locked.

Lime-years ahead

Online-surveys for every purse and purpose