Cart Buy Licenses Contact Integrate into App Docs Support Login/Register
v 1.8.67
v 1.8.67
Overview Survey Creator Examples Docs Source Download

Avoid adding duplicated questions

Use onQuestionAdded event to avoid duplicating questions in survey


                        
    
        SurveyCreator.StylesManager.applyTheme("bootstrap");
    

// register property named 'tag'
Survey
    .Serializer
    .addProperty("question", {
        name: "tag",
        category: "general"
    });

var creatorOptions = {questionTypes : [ "text" ]};
var creator = new SurveyCreator.SurveyCreator("creatorElement", creatorOptions);
creator.showToolbox = "right";
creator.showPropertyGrid = "right";
creator.rightContainerActiveItem("toolbox");

// assign 'tag' property to some unique value
creator
    .toolbox
    .addItem({
        name: "countries",
        isCopied: true,
        iconName: "icon-default",
        title: "All countries",
        json: {
            "type": "dropdown",
            optionsCaption: "Select a country...",
            choicesByUrl: {
                url: "https://restcountries.eu/rest/v2/all"
            },
            tag: "myItem1"
        }
    });

// Check if existing questions have same tag and cancel add operation
creator.onQuestionAdded.add(function(sender, options) {
    if (options.question.tag) {
        var questions = sender.survey.getAllQuestions();
        for (var i = 0; i < questions.length; i++) {
            if (questions[i] !== options.question && questions[i].tag === options.question.tag) {
                options.question.delete();
                return;
            }
        }
    }
});;
                    
<!DOCTYPE html>
<html lang="en">
<head>
    <title>Use onQuestionAdded event to avoid duplicating questions in survey, Survey Creator Example</title>

<meta name="viewport" content="width=device-width" />
    <script src="https://unpkg.com/knockout@3.5.1/build/output/knockout-latest.js"></script>
    <script src="/DevBuilds/survey-knockout/survey.ko.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/ace/1.4.10/ace.min.js" type="text/javascript" charset="utf-8"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/ace/1.4.10/ext-language_tools.js" type="text/javascript" charset="utf-8"></script>
    <!-- Uncomment to enable Select2
    <script src="https://unpkg.com/jquery"></script>
    <link href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.3/css/select2.min.css" rel="stylesheet" />
    <script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.3/js/select2.min.js"></script>
    -->
        <script src="/DevBuilds/survey-creator/survey-creator.min.js"></script>
        <link href="/DevBuilds/survey-creator/survey-creator.min.css" type="text/css" rel="stylesheet" />
    <link rel="stylesheet" href="https://unpkg.com/bootstrap@3.3.7/dist/css/bootstrap.min.css">
    <link rel="stylesheet" href="./index.css">

</head>
<body>
    
<div id="surveyContainer">
        <div id="creatorElement"></div>
</div>

<script type="text/javascript" src="./index.js"></script>

</body>
</html>

This sample demonstrates how to handle the onQuestionAdded event to avoid adding duplicated questions onto the survey designer surface.