Question T12667
Visible to All Users

Textbox values on basis of other dropdown

created 2 years ago

Can we set a value of textbox on the basis of other dropdown value??
Suppose I have a dropdown Country with list of country and another textbox which will have ISOCODE in it.
The dropdown api for country has countryName and Iso_code init in response.
I want to set dropdown as countryName and the text box value should be autoselected on the basis of country selection.

Show previous comments (7)

    I guess you are still not getting my point the text box should display the region and the dropdown should be as it is with countryname

      Hello Abhishek,
      It appears I actually do not completely follow. To help me move forward and consider a suitable option, please send me an example/explain the current behavior and the desired output.

      Please feel free to share a sample JSON data (or use "url": "https://surveyjs.io/api/CountriesExample" from my sample) and clarify:

      • Which data source field should be used as a Dropdown question's value (i.e. saved into survey results);
      • Which data source field should be used as a Dropdown's display value (displayed in the drop-down list);
      • Which data source field should be displayed in a Text box.

      Thank you for your cooperation in advance.

        {
            "type": "dropdown",
            "name": "country",
            "title": "Select a country",
            "description": "A full list of countries is queried from a RESTful web service.",
            "choicesByUrl": {
              "url": "https://surveyjs.io/api/CountriesExample",
              "valueName": "name",
              "titleName": "officialName"
            }
          }, {
            "type": "text",
            "name": "region"
          }

        Take above json example
        I want country dropdown to show OfficialName and store value to database is Name column.
        Where else in the text region I want to show the value of region from the response of country dropdown selected
        Suppose I selected below value from country

        Code
        {"name":"Afghanistan","officialName":"Islamic Republic of Afghanistan","region":"Asia","cca2":"AF","ccn3":"AF","cca3":"AFG","cioc":"AFG"}

        It will show dropdown value as "Islamic Republic of Afghanistan" and store "Afghanistan" in database.
        But in the textbox region it should show Asia as region.

        And if I change the value it should reflect the respective region of dropdown value.

        Answers approved by surveyjs Support

        created 2 years ago

        Hello Abhishek,
        I greatly appreciate this clarification.

        To bind a Text box to an arbitrary property of a selected Dropdown item, please follow these steps:

        JavaScript
        "choicesByUrl": { "url": "https://surveyjs.io/api/CountriesExample", "valueName": "name", "titleName": "officialName", "attachOriginalItems": true, }

        Now, the original object is attached to a selected Dropdown option.

        • Register a custom function which would obtain a custom property of a selected Dropdown option.
        JavaScript
        function getRegion(props){ let dropdown = this.survey.getQuestionByName(props[0]); if(!!dropdown.selectedItem){ return dropdown.selectedItem.originalItem.region; } } Survey.FunctionFactory.Instance.register('getRegion', getRegion);
        • Use the custom function in a defaultValueExpression property of a single-input Text field:
        JavaScript
        { "type": "text", "name": "region", "defaultValueExpression": "getRegion('country')" }

        Example.

        Please let me know if you have any questions or require further assistance.

          Comments (2)

            Thanks Jane this works

              My pleasure. Please feel free to contact us at any time.