Modify New Question

Edit in →

Survey Creator raises an onQuestionAdded event when users add questions to a survey. You can handle this event to modify the questions on the fly. In this demo, the onQuestionAdded event is used to generate GUIDs and use them as question names.

Implementation

A function that handles the onQuestionAdded event accepts an options object as the second parameter. The object's question property provides access to the question being added, while the reason property contains a string value indicating how the question was added: dragged from the Toolbox ("DROPPED_FROM_TOOLBOX"), created using the Add Question button ("ADDED_FROM_PAGEBUTTON"), duplicated ("ELEMENT_COPIED"), or converted from another question type ("ELEMENT_CONVERTED"). In this demo, the onQuestionAdded function changes the question's name property to a GUID and title property to a predefined value. These changes do not apply if the question is converted from another type, because in this case the question already has a GUID and a proper title.

var questionCounter = 1;
creator.onQuestionAdded.add((_, options) => {
  if (options.reason === "ELEMENT_CONVERTED")
    return;
  
  const q = options.question;
  q.name = guid();
  q.title = "Question " + questionCounter;
  questionCounter++;
});

To ensure that users don't change the generated GUIDs, make the name property read-only using the Serializer API:

import { Serializer } from "survey-core";
Serializer.findProperty("question", "name").readOnly = true;

Survey Creator uses question names in the UI. Now that the question names are GUIDs, they become hard to read and distinguish at a glance. To eliminate this disadvantage, enable the showObjectTitles and showTitlesInExpressions properties when you instantiate Survey Creator. With these settings, the Survey Creator UI will use question titles instead of the names.

const creatorOptions = {
  // ...
  showObjectTitles: true,
  showTitlesInExpressions: true
}

Similar Events

In addition to onQuestionAdded, Survey Creator raises other events that allow you to modify newly added survey elements dynamically. These events are handled similarly to onQuestionAdded.

Event name Raised when
onPanelAdded Raised when users add a panel to the survey.
onPageAdded Raised when users add a page to the survey.
onMatrixColumnAdded Raised when users add a column to a Multi-Select or Dynamic Matrix.
onItemValueAdded Raised when users add a new item value (column, row, choice).

Your cookie settings

We use cookies on our site to make your browsing experience more convenient and personal. In some cases, they are essential to making the site work properly. By clicking "Accept All", you consent to the use of all cookies in accordance with our Terms of Use & Privacy Statement. However, you may visit "Cookie settings" to provide a controlled consent.

Your renewal subscription expires soon.

Since the license is perpetual, you will still have permanent access to the product versions released within the first 12 month of the original purchase date.

If you wish to continue receiving technical support from our Help Desk specialists and maintain access to the latest product updates, make sure to renew your subscription by clicking the "Renew" button below.

Your renewal subscription has expired.

Since the license is perpetual, you will still have permanent access to the product versions released within the first 12 month of the original purchase date.

If you wish to continue receiving technical support from our Help Desk specialists and maintain access to the latest product updates, make sure to renew your subscription by clicking the "Renew" button below.