Search code examples
javascriptcheckboxqualtrics

Qualtrics JavaScript Add Checkbox to Flag Questions


I am creating a pilot survey in Qualtrics to make sure all my questions are clear and easy to understand. For each question, I want to add a checkbox or some other feature so respondents can flag the question as badly-worded or difficult to understand. The checkbox would appear below the answer choices or text boxes. Is there a way to do this via the JavaScript editor in Qualtrics?

I don't want to ask a separate question after each question to ask if the previous question was unclear. It would take up too much space and annoy respondents.

The solution I came up with so far is to create a checkbox before each question and save whether the checkbox was clicked to embedded data.

question

First, I create an embedded data field named check1_d in Survey Flow.

Second, I embed a checkbox into the HTML of the question text.

<input id="check1" type="checkbox" value="1" /> <p>What is your favorite color?<p>

Finally, I add the following to the JavaScript editor to record the answer to my embedded data field check1_d. But this part is not working. Does anyone know how to record whether people clicked on the box to the embedded data field?

var mycheck = String(document.getElementById("check1").check);
Qualtrics.SurveyEngine.setEmbeddedData("check1_d", mycheck);

Solution

  • incaren is correct on the .checked attribute, but I think you have other issues. You need an event listener, otherwise you are just saving the initial status of the checkbox. Also, you need to define the embedded variable check1_d in the survey flow, before the question block.

    Below is code that will work. Since Qualtrics uses prototypejs, the code below uses it. Instead of saving "true" and "false" as strings, it saves 1 for true and 0 for false.

    Qualtrics.SurveyEngine.addOnload(function() {
        var mycb = $('check1');
        Qualtrics.SurveyEngine.setEmbeddedData("check1_d", +mycb.checked);
        mycb.on('click', function (event, element) {
            Qualtrics.SurveyEngine.setEmbeddedData("check1_d", +element.checked);
        }); 
    });