Question T4216
Visible to All Users

survey creator - Dropdown web options, always select 1st option as default.

created 5 years ago

Hello team,

how can we configure a dropdown to select first option by default? when options are from a webservice.

we want to be configurable from builder. you think i can create custom property and handle it? or any simpler way?

Thanks,
A A Navaneeth

Answers approved by surveyjs Support

created 5 years ago

Hello,
You have to do it on survey level using survey.onLoadChoicesFromServer event:

JavaScript
survey.onLoadChoicesFromServer.add(function(sender, options) { if(options.choices.length == 0) return; survey.question.value = options.choices[0].value; });

Thank you,
Andrew
SurveyJS Team

    Show previous comments (1)

      Do you mean you want this functionality just for some dropdown questions?
      Then add a new property into dropdown:

      JavaScript
      Survey.Serializer.addProperty("dropdown", {name: "selectFirstPropertyByDefault:boolean", category: "choices });

      and then correct your code for survey running:

      JavaScript
      survey.onLoadChoicesFromServer.add(function(sender, options) { if(options.choices.length == 0 || !options.question.selectFirstPropertyByDefault) return; survey.question.value = options.choices[0].value; });

      Thank you,
      Andrew
      SurveyJS Team

        Thank you. this works!!

          You are welcome!

          Thank you,
          Andrew
          SurveyJS Team