Question T12922
Visible to All Users

Dropdown question title in expressions

created 2 years ago

Hi,

Is there a way to have a dropdown title name available to be used in expressions?

The purpose here is to have an expression question to display the title value of another dropdown question.

I need to save both values of the selected item in the dropdown without using JS code, just with configuration within the survey JSON configuration.

Many thanks in advance.

Best regards
Rogério Vicente

Answers approved by surveyjs Support

created 2 years ago

Hello Rogério,
If you wish to reuse a question title in expression, I recommend that you create a survey variable or calculated value and use it in a title of the Dropdown question, and in an Expression question as well. Please consider this example:

JavaScript
survey.setVariable('var1', 'Hello World!'); const json = { "pages": [ { "name": "page1", "elements": [ { "type": "dropdown", "name": "question1", "title": "Dropdown question title: {var1}", "choices": [ "Item 1", "Item 2", "Item 3" ] }, { "type": "expression", "name": "question2", "expression": "'This is a survey variable: ' + {var1}" } ] } ] }

Example
Clipboard-File-1.png

Please let me know if this option works for you.

    Show previous comments (5)

      Hello Rogério,
      Thank you for the update.

      In reference to your original question: if you wish to use a Dropdown question value in expressions, you can wrap a Dropdown question name to curly braces and use it in expressions: {question1}. For more information on this option, refer to the following tutorial: Conditional Logic and Dynamic Texts.

      If we are talking about saving the value and text settings of a selected Dropdown option, I'm afraid we don't have an option available out of the box. At the same time, you can use the getPlainData function to get detailed information about user responses on survey completion. To save an item's text along with a value, you just need to write the code similar to the following:

      JavaScript
      survey.onComplete.add((sender, options) => { const resultData = []; for (const key in sender.data) { const question = sender.getQuestionByName(key); if (!!question) { const item = { name: key, value: question.value, title: question.displayValue }; resultData.push(item); } } // Save resultData });

      The output will be the following:

      JSON
      [ { "name": "car", // A question name "value": 3, // A selected item's value "title": "Audi" // A selected item's display text } ]

      I created this example for your reference: https://plnkr.co/edit/HwiE5LXir3ZX3GHr.

      Please let me know if any questions remain.

      RV RV
      Rogério Vicente 2 years ago

        Hi Jane,

        It is the second option, I believe I did not explain it succeeded explaining it initially, sorry for this.

        We have to change a few things in our platforms, but we'll go for the solution you mention. Many thanks for your cooperation!

        Best regards
        Rogério Vicente

          You're very welcome, Rogério.

          Please feel free to contact us if you have any questions or require further assistance.