In Qualtrics polls, can you save the response to an item in the embedded data when you click the next button?

I actually spent a lot of time figuring out the answers to this question, putting it together from spotty documentation and a lot of checks in the web console. Since, of course, the most useful information came from Stack Overflow, I wanted to ask this question and answer this question in the future for anyone else who is looking.

The question is, how can you add javascript to the question that will immediately save this question in the embedded data field? Immediately, as in, without waiting for the next Qualtrics.SurveyEngine.AddOnLoad (). This is due to the question of how to add javascript to the survey question and run it when you click Next. And the question is, how can you save inline data fields that are dynamically called based on the question id?

Caveat Emptor: All of these JavaScript hacks are clearly not supported by Qualtrics, so the code here may eventually stop working.

+4
source share
3 answers

, . , . , , , - .

Qualtrics.SurveyEngine.addOnload(function()
{
    // everything here runs when the page is loaded. So, we can get the Question ID here,
    // but we can't get the response value until later.
    var currentQuestionID = this.getQuestionInfo().QuestionID
    //var resultEmbeddedName = currentQuestionID + "_result"  //e.g. QID6_result 
    var resultEmbeddedName = "result_" + currentQuestionID.substring(3)   //e.g. result_6

    $('NextButton').onclick = function (event) {
        // everything in here will run when you click the next button
        // note that it has access to javascript variables from the enclosing function
        // however, if you declare something in here with "var" then it will be a LOCAL variable

        // the following alert will appear when you click the next button. For me, it appears twice;
        // I'm not sure why.

        // Save the current question response value
        var responseTextField = document.getElementById(currentQuestionID + '~1~result')
        var currentResponse = responseTextField.value
        alert("Result: " + currentResponse + "\nwill be available to future questions as: \n$" + "{e://Field/" + resultEmbeddedName + "}")

        Qualtrics.SurveyEngine.setEmbeddedData(resultEmbeddedName, currentResponse)

        // and now run the event that the normal next button is supposed to do
        Qualtrics.SurveyEngine.navClick(event, 'NextButton')
    }

});

. , , . , "currentResponse" , NUMBER , "currentChoiceText" , (, "" "" ). , , .

Qualtrics.SurveyEngine.addOnload(function()
{
    // everything here runs when the page is loaded. So, we can get the Question ID here,
    // but we can't get the response value until later.
    var currentQuestionID = this.getQuestionInfo().QuestionID
    console.log("Current Question ID is: " + currentQuestionID)
    //var resultEmbeddedName = currentQuestionID + "_result"  //e.g. QID6_result 
    var resultEmbeddedName = "result_" + currentQuestionID.substring(3)   //e.g. result_6

    $('NextButton').onclick = function (event) {
        // everything in here will run when you click the next button
        // note that it has access to javascript variables from the enclosing function
        // however, if you declare something in here with "var" then it will be a LOCAL variable

        // the following alerts will appear when you click the next button. For me, it appears twice;
        // I'm not sure why.

        // Save the current question response value

        var questionObject = Qualtrics.SurveyEngine.getInstance(currentQuestionID)
        var currentResponse = questionObject.getSelectedChoices()[0] //in case more than one is selected, it will only work here to take one!   
        var theQuestionInfo=questionObject.getQuestionInfo()
        var choicesObject=theQuestionInfo.Choices
        var thisChoiceObject=choicesObject[currentResponse]
        var currentChoiceText=thisChoiceObject.Text

        console.log("Number of the current choice is " + currentResponse)
        console.log("Text of the current choice is " + currentChoiceText)


        alert("Result: " + currentChoiceText + "\nwill be available to future questions as: \n$" + "{e://Field/" + resultEmbeddedName + "}")

        Qualtrics.SurveyEngine.setEmbeddedData(resultEmbeddedName, currentChoiceText)

        // and now run the event that the normal next button is supposed to do
        Qualtrics.SurveyEngine.navClick(event, 'NextButton')
    }

});

, , . javascript-.

, javascript. ( , "" ). , ${e://Field/result_1}, . , , , .

, , , . - , , , .

, , "", .

+6

$('NextButton'). onclick $('NextButton'). onclick . , onclick , .

// and now run the event that the normal next button is supposed to do
Qualtrics.SurveyEngine.navClick(event, 'NextButton')
0

This works every time you click the "Next" button. Hope this helps!

Qualtrics.SurveyEngine.addOnload(function(){
var nextButton =document.getElementById("NextButton");
if (nextButton!=null){
    $("NextButton").on("click", function(event) {

    alert("Hello World!");
    });
  } 
});
0
source

Source: https://habr.com/ru/post/1534939/


All Articles