How to use Javascript and embedded data to store data from a checkbox added using html? | XM Community
Skip to main content

I have added a couple of checkboxes throughout my survey (Simple layout) using the html code <input type="checkbox">.

I have added print screens of a couple of examples below:

Example 1: Checkboxes to indicate why a particular section of questions is not relevant for the respondent to complete:

Example 2: Check box after a statement on a matrix table question to indicate that the particular statement is not relevant to the respondent: 


I need these checkboxes to record data. From searching the community posts I can see I need to use Javascript to store the checkbox status in an embedded data. However I am a complete noob and have never used Javascript or Embedded data before. Is anyone able to help me with how I do this?

 

Thanks so much!

@molliep,

You can add an id to your checkbox elements:

<input type="checkbox" id="complications">

Then use JS to add an event handler to a checkbox:

Qualtrics.SurveyEngine.addOnload(function() {
setCbEdOnclick("complications");
});

function setCbEdOnclick(id) {
document.getElementById(id).addEventListener("click", function() {
if(this.checked) Qualtrics.SurveyEngine.setJSEmbeddedData(this.id, "1");
else Qualtrics.SurveyEngine.setJSEmbeddedData(this.id, "0");
});
}

The setCbEdOnclick function can be used for any checkbox. You could add it to the header or footer (inside a <script></script> tag) to make it available on any page in the survey.  The embedded data fields and checkbox ids will match.  Prefix the embedded data fields with __js_ when used in the survey flow (e.g., __js_complications). The embedded data fields must be initialized in the survey flow to be saved in response data.


Thanks so much 🙂 As I have more than one checkbox within one question, how would this affect the JavaScript? (Sorry, complete noob! I’m unsure if I can just copy and paste the same thing twice and change the id’s)


Leave a Reply