Question T12679
Visible to All Users

Textbox or dropdown show the first value from the choice

created 2 years ago

{
"type": "text",
"name": "tdc_no",
"title": "TDC #",
"hideNumber": true,
"choicesByUrl": {
"url": location.origin + "/Enquiry/grade/{grade}",
"valueName": "tdcNumber",
"titleName":"tdcNumber"
}
, "defaultValue": "{{choicesByUrl}}[0].value"
},

I want this to show the first choice which is there in the response list as the value of the textbox/dropdown

Comments (1)

    any help?

    Answers approved by surveyjs Support

    created 2 years ago

    Hello Abhishek,
    You can handle the survey.onLoadChoicesFromServer function and set a question's value to the first item in the array of choices:

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

    Please let me know if you have further questions.

      Show previous comments (9)

        Hello Abhishek,
        Yes, I believe updating a Textbox value in the survey.onValueChanged may help. Please try the following demo: https://plnkr.co/edit/00jEVLv0QDfVcwbj.

        JavaScript
        survey.onLoadChoicesFromServer.add((sender, options) => { let firstItem = options.choices[0]; options.question.value = firstItem; let textbox = sender.getQuestionByName('region'); textbox.value = firstItem.originalItem.region; }); survey.onValueChanged.add((sender, options) => { if(options.name === 'country'){ if(!!options.question.selectedItem && !!options.question.selectedItem.originalItem){ let textbox = sender.getQuestionByName('region'); textbox.value = options.question.selectedItem.originalItem.region; } } })

        Run the example and update a selected Dropdown option; notice that the Textbox displays the region value of a selected option. Note that I reset the defaultValueExpression for the Textbox; now, the Textbox value is updated in the onValueChanged callback. Please let me know if this code works for you.

          Cool Thanks this works can you check my above comment and provide a solution for the same?

            Hello Abhishek,
            Thank you for the update. I'm happy to hear that this code works for you.
            To process your additional issue in the most efficient manner, I created a separate ticket on your behalf: T12700: How to set the "thickness" textbox in ValueChange of a dropdown which is basically a calculated value. We placed it in our processing queue and will process it shortly.