Cart Buy Licenses Contact Support Login/Register
v 1.8.6
v 1.8.6
Overview Survey Creator Examples Docs Source Download

Modify Designer and Test Surveys

Use onSurveyInstanceCreated event to modify Surveys inside Creator


                        
    
        SurveyCreator.StylesManager.applyTheme("bootstrap");
    

Survey
    .Serializer
    .addProperty("question", "popupdescription:text");


        
            var creatorOptions = { };
            var creator = new SurveyCreator.SurveyCreator("creatorElement", creatorOptions);
            creator.showToolbox = "right";
            creator.showPropertyGrid = "right";
            creator.rightContainerActiveItem("toolbox");
        
function createButtonForPopup(options, showPopup) {
    //Return if there is no description to show in popup
    if (!options.question.popupdescription)
        return;

    //Add a button;
    var btn = document.createElement("button");
    btn.type = "button";
    btn.className = "btn btn-info btn-xs";

    btn.style.position = "absolute";
    btn.style.marginLeft = "20px"

    btn.innerHTML = "More Info";
    if(showPopup) {
        var popupdescription = options.question.popupdescription;
        btn.onclick = function () {
            alert(popupdescription);
        }
    }
    var header = options
        .htmlElement
        .querySelector("h5");
    var span = document.createElement("span");
    span.innerHTML = "  ";
    header.appendChild(span);
    header.appendChild(btn);
}
function renderQuestionInDesigner(survey, options) {
    createButtonForPopup(options, false);
}
function renderQuestionInTestSurvey(survey, options) {
    createButtonForPopup(options, true);
}
//Calls when creator needs to create a new survey instance.
//In most cases you need to check options.reason for "designer" and "test".
//Survey is created for designer survey or "Test Survey" tab.
//Several properties editors create survey as well. In this case the reason will be: "conditionEditor", "defaultValueEditor", "restfulEditor"
creator.onSurveyInstanceCreated.add(function(sender, options) {
    //If we are creating a surface for designer surface
    if(options.reason == "designer") {
        options.survey.onAfterRenderQuestion.add(renderQuestionInDesigner);
    }
    //If we are creating a surface for "Test Survey" tab
    if(options.reason == "test") {
        options.survey.onAfterRenderQuestion.add(renderQuestionInTestSurvey);
    }
});

//If there is no JSON, then call creator.JSON = {}; to re-create the designer survey and call onSurveyInstanceCreated event.
creator.JSON = {
    title: "Software developer survey.",
    pages: [
        {
            title: "What operating system do you use?",
            questions: [
                {
                    type: "checkbox",
                    name: "opSystem",
                    title: "OS",
                    hasOther: true,
                    isRequired: true,
                    popupdescription: "If you do not use any of the listed operating system, please select 'others' and type your operating system name.",
                    choices: ["Windows", "Linux", "Macintosh OSX"]
                }
            ]
        }, {
            title: "What language(s) are you currently using?",
            questions: [
                {
                    type: "checkbox",
                    name: "langs",
                    title: "Please select from the list",
                    popupdescription: "Select all programming languages you have been using for the last six months.",
                    colCount: 4,
                    isRequired: true,
                    choices: [
                        "Javascript",
                        "Java",
                        "Python",
                        "CSS",
                        "PHP",
                        "Ruby",
                        "C++",
                        "C",
                        "Shell",
                        "C#",
                        "Objective-C",
                        "R",
                        "VimL",
                        "Go",
                        "Perl",
                        "CoffeeScript",
                        "TeX",
                        "Swift",
                        "Scala",
                        "Emacs Lisp",
                        "Haskell",
                        "Lua",
                        "Clojure",
                        "Matlab",
                        "Arduino",
                        "Makefile",
                        "Groovy",
                        "Puppet",
                        "Rust",
                        "PowerShell"
                    ]
                }
            ]
        }, {
            title: "Please enter your name and e-mail",
            questions: [
                {
                    type: "text",
                    name: "name",
                    title: "Name:",
                    popupdescription: "Please, type your name as 'Given Name' 'Family Name'."
                }, {
                    type: "text",
                    name: "email",
                    title: "Your e-mail",
                    popupdescription: "Please, make sure you do not misspell your e-mail."
                }
            ]
        }
    ]
};
                    
<!DOCTYPE html>
<html lang="en">
<head>
    <title>Use onSurveyInstanceCreated event to modify Surveys inside Creator, Survey Creator Example</title>

<meta name="viewport" content="width=device-width" />
    <script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.4.0/knockout-min.js"></script>
<script src="https://surveyjs.azureedge.net/1.8.6/survey.ko.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>
    -->
    <link href="https://surveyjs.azureedge.net/1.8.6/survey-creator.css" type="text/css" rel="stylesheet" />
    <script src="https://surveyjs.azureedge.net/1.8.6/survey-creator.js"></script>
    <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>