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

Set and save survey

Show how to set and save a survey


                        


        
            var creatorOptions = { };
            var creator = new SurveyCreator.SurveyCreator("creatorElement", creatorOptions);
            creator.showToolbox = "right";
            creator.showPropertyGrid = "right";
            creator.rightContainerActiveItem("toolbox");
        
//Automatically save survey definition on changing. Hide "Save" button
creator.isAutoSave = true;
//Show state button here
creator.showState = true;

var localStorageName = "SaveLoadSurveyCreatorExample";
//Setting this callback will make visible the "Save" button
creator.saveSurveyFunc = function(saveNo, callback) {
    //save the survey JSON
    console.log(creator.text);
    //You can store in your database JSON as text: creator.text  or as JSON: creator.JSON
    window.localStorage.setItem(localStorageName, creator.text);
    
    //We assume that we can't get error on saving data in local storage
    //Tells creator that changing (saveNo) saved successfully.
    //Creator will update the status from Saving to saved
    callback(saveNo, true); 
}

var defaultJSON = { pages: [{ name:'page1', questions: [{ type: 'text', name:"q1"}]}]};
creator.text = window.localStorage.getItem(localStorageName) || JSON.stringify(defaultJSON);
//If you get JSON from your database then you can use creator.JSON property
//creator.JSON = yourJSON;
                    
<!DOCTYPE html>
<html lang="en">
<head>
    <title>Show how to set and save a survey, 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.12/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>
    -->
    <link href="https://surveyjs.azureedge.net/1.8.12/survey-creator.min.css" type="text/css" rel="stylesheet" />
    <script src="https://surveyjs.azureedge.net/1.8.12/survey-creator.min.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>