Capturing response from text entry in loop & merge using JavaScript | XM Community
Skip to main content

Hi all. I want to start out by saying that there seems to be a very similar problem to what I have but I can't figure out why it won't work for me. https://community.qualtrics.com/XMcommunity/discussion/19749/capture-and-embed-data-within-a-loop

The set up:
Block 1: Some regular questions

  • Q1: How many people have you hung out with in the past two days? (dropdown 1-10)

Block 2: Loop & Merge (based on answer to Q1)
  1. Q2: Who have you hung out with?

  2. Tim

  3. Bob

  4. Joe

  5. New Person

  6. Q3: [IF "New Person" SELECTED IN Q2] What is the name of this new person? [text entry]


The goal:
Eventually, I need to set embdedded data for any new persons in the loop and merge, but for now I'm simply trying to find a way to even capture the response in javascript which seems like it should be easy.

My javascript:
Right now I have the following javascript on Q3:

Qualtrics.SurveyEngine.addOnUnload(function()
{
/*Place your JavaScript here to run when the page is unloaded*/
let text = jQuery('#QR~'+this.questionId+" input[type=text]").eq(0).val()
console.log(text)
});

I have also tried:

Qualtrics.SurveyEngine.addOnUnload(function()
{
/*Place your JavaScript here to run when the page is unloaded*/
let loopNumber = "${lm://CurrentLoopNumber}"
let qId = this.questionId
let embeddedDataFieldname = "textEntry_"
embeddedDataFieldname = embeddedDataFieldname +loopNumber
console.log('QR~'+qId)
let text = document.getElementById('QR~'+qId).value
console.log(text)
/*Qualtrics.SurveyEngine.setEmbeddedData(embeddedDataFieldname, text);*/
});

Both times, it tells me "null is not an object"
Am I missing something here?

Qualtrics.SurveyEngine.addOnPageSubmit(function(type)
{
if(type == "next")
{
var text_val=jQuery('input[type="text"]').val();
}
});

The above code should be getting the value in the textbox which you can then set using setEmbeddedData.


Leave a Reply