Clearing text from a previously selected text box in multiple choice question. | XM Community
Skip to main content

I have a multiple choice question with 3 choices. Each choice has a text box. When someone selects a choice, adds something in the text box then changes their mind and selects another choice and adds text, the text from the 1st selection is still visible. 
I need the text to clear from a previous selection when another selection is made.

Hi there,

the below code should delete the other texts of unselected choices

you need to adopt the QID to your needs and also the numbers in your condition and in the get elementById function

Qualtrics.SurveyEngine.addOnReady(function()
{
 this.questionclick = function(event,element){
  let selectedChoices = this.getSelectedChoices();
  if(selectedChoices.include('1') === false){
  document.getElementById("QR~QID4~1~TEXT").value=''
  }
  
 if(selectedChoices.include('2') === false){
  document.getElementById("QR~QID4~2~TEXT").value=''
 }
  
  if(selectedChoices.include('3') === false){
  document.getElementById("QR~QID4~3~TEXT").value=''
  }
  
  }

});

Best regards

Rudi


Hi Rudi
Thank you for suggestion - from my very rookie perspective it looks like it should work but unfortunately it didn't work for me.
I am a total newbie at qualtrics and am more than likely entering the wrong things for the numbers and the ElementByld function.
My QID is B1_iscids-trigoften.
I'm not sure what you mean by the numbers in my condition. I don't have numbers displayed so I entered the name of the choice (e.g. 'Times per Week'). I also tried putting a number in front of the choice (e.g. '1. Times per Week') but that didn't work either. I also used the name of the choice in the ElementByld (i.e. Times per WEEK.)
I have attached a picture of the question in Edit mode, then one from preview mode that shows the text still in the 1st selection made.
I have custom validation to ensure they answer with a number in their selected choice (picture attached).
This is the code I entered:
Qualtrics.SurveyEngine.addOnReady(function()

{

 this.questionclick = function(event,element){

 let selectedChoices = this.getSelectedChoices();

 if(selectedChoices.include('Times per WEEK') === false){

 document.getElementById("QR~B1_iscids_trigoften~Times per WEEK~TEXT").value=''

 }

  

 if(selectedChoices.include('Times per MONTH') === false){

 document.getElementById("QR~B1_iscids_trigoften~Times per MONTH~TEXT").value=''
 }

  

 if(selectedChoices.include('Times per YEAR') === false){

 document.getElementById("QR~B1_iscids_trigoften~Times per YEAR~TEXT").value=''

 }

  

 }



});

Screen Shot 2022-02-01 at 9.25.08 am.pngScreen Shot 2022-02-01 at 9.29.52 am.pngScreen Shot 2022-02-01 at 9.34.26 am.pngThank you so much again Rudi. I really do appreciate your help.


Qualtrics.SurveyEngine.addOnUnload(function()
{
/*Place your JavaScript here to run when the page is unloaded*/

});


Hi there, the QID is not the name you give the question.
the best and easiest way to find the QID is to use piped text and then you should see in the editor something like ${q://QID4/SelectedChoicesRecode}. QID4 would be in this example the system QuestionID

Best regards

Rudi


Thank you Rudi.
For my 3 multiple choice questions, this was in the piped text editor:
Choice 1: ${q://QID437/ChoiceDescription/4}
For choice 1; QID is 437; Selected choice is 1; ElementByld is 4?
Choice 2: ${q://QID437/ChoiceDescription/5}
For choice 2; QID is 437; Selected choice is 2; ElementByld is 5?
Choice 3: ${q://QID437/ChoiceDescription/6}
For choice 3; QID is 437; Selected choice is 3; ElementByld is 6?
Could you please confirm that the script would read-
Qualtrics.SurveyEngine.addOnReady(function()
{
 this.questionclick = function(event,element){
  let selectedChoices = this.getSelectedChoices();
  if(selectedChoices.include('1') === false){
  document.getElementById("QR~QID437~4~TEXT").value=''
  }
 if(selectedChoices.include('2') === false){
  document.getElementById("QR~QID437~5~TEXT").value=''
 }
  if(selectedChoices.include('3') === false){
  document.getElementById("QR~QID437~6~TEXT").value=''
  }
  }
});
One last question, does the Elementbyld number relate to the recoded values, or is the Elementbyld number set by qualtrics with no relationship to the recoded values?
Thank you so much for your help again Rudi. As you can see, I am definitely a qualtrics newbie and have a long way to go.


Hey,
you would find the correct number when looking at the selected choices. However I would assume that you are right. Have to tried the above code already?

Best regards

Rudi


I just tried it out Rudi and it works beautifully - thank you so much.
PS Where do I find 'selected choices' to find the correct number? I assumed that because I had 3 questions my choices would be 1, 2, or 3. I would love to know the proper way to find them please.


https://community.qualtrics.com/XMcommunity/discussion/comment/43301#Comment_43301The code works beautifully Rudi.
This is my first foray into the community. I hope others find your script for this. I searched for it everywhere (spent hours) and I think others probably need it too. I hope they make their way to your answer.


This is the code from Rudi that works perfectly - thanks again Rudi.
*******the below code deletes the other texts of unselected choices*******

you need to adopt the QID to your needs and also the numbers in your condition and in the get elementById function

Qualtrics.SurveyEngine.addOnReady(function()
{
 this.questionclick = function(event,element){
  let selectedChoices = this.getSelectedChoices();
  if(selectedChoices.include('1') === false){
  document.getElementById("QR~QID4~1~TEXT").value=''
  }
  
 if(selectedChoices.include('2') === false){
  document.getElementById("QR~QID4~2~TEXT").value=''
 }
  
  if(selectedChoices.include('3') === false){
  document.getElementById("QR~QID4~3~TEXT").value=''
  }
  
  }

});


Leave a Reply