Using Javascript to set a multiple choice (multi-select) response | XM Community
Skip to main content

I am trying to use js to set a response to a survey question based on the response to a prior question. The question I need to set the response for, however, is set to not display (via DisplayLogic) based on the answer to the initial question.
For example (in plain language):
Question1:

  • Answer A

  • Answer B

If the user chooses Answer A, they are sent to the end of the survey.
Question2 - multiple choice, multi-select:
  • Answer1

  • Answer2

  • Answer3

  • Answer4

I would like to set the response to this question as Answer4 if the user chose AnswerA in Q1 and never saw this question.
Is this possible?

Yes, this is possible. You will need to auto-punch this choice if choice A is selected in Q1.
You may do the same using JS with the following code.
//to punch choice A in Q2 use the below code
jQuery('input[type="checkbox"]').eq(0).prop('checked',true)
//You will need to hide the page and click next if choice A is selected in Q1 for which you will need to pipe-in the selected choice in Q1 in Question Q2 and use if logic to check if choice A was selected and if it's true then use the above code to punch the choice and then hide the page using below code and click the next button.
//to hide the page use below code
jQuery('#Wrapper').css('opacity','0');
//to click next button use below code
jQuery("#NextButton").click()

Hope this helps!


https://community.qualtrics.com/XMcommunity/discussion/23632/using-javascript-to-set-a-multiple-choice-multi-select-responseYou can use embedded data via branch logic to populate the answer if question is answered or if not. And use that embedded data as the question's response in data analysis.
If you need that second question to be checked that would require custom coding as well as question on same page.
Hope it helps!


I wouldn't recommend JS here. Considering this is just for your backend, it would be cleaner and more flexible to just create a new variable in the data and analysis tab. Set it to the value of Q2 or answer 4 if Q2 is empty.


https://community.qualtrics.com/XMcommunity/discussion/comment/54274#Comment_54274Can you provide more details on this. please?


Thank you, ahmedA . Adding a variable may not work for this situation. It is an existing survey we reuse periodically throughout the year. It has several years of data in it already. We are currently running the raw data through Tableau Prep for cleaning and organizing. I can accomplish what is needed in Tableau Prep, but we are trying to see if we can get the survey to handle it before it even gets to Tableau Prep.
Introducing a new variable at this point would require updates to many downstream processes. I really need a way to make sure that question (in my example, Q2) has that answer when the question is not displayed (and the criteria meets one of the pieces of embedded data).


grahulp5 - your solution almost gets me there. But the jQuery to click the next button is not working. I even tried setting a delay.


https://community.qualtrics.com/XMcommunity/discussion/comment/54298#Comment_54298 What I mean here is create an embedded data and have a branch logic below that question if it is answered opt1 you include an embedded data element with static text and end of survey and use the same embedded data to fill the choice if opt 2 is answered.
Hope it helps!


https://community.qualtrics.com/XMcommunity/discussion/comment/54313#Comment_54313Thank you! This accomplished what I needed and no javascript necessary.


Leave a Reply