How to activate a text box to show up when a MC selection is made | XM Community
Skip to main content

How to activate a text box to show up when a MC selection is made

  • December 20, 2022
  • 6 replies
  • 1234 views

Hello,
I am setting up a multiple choice question and would like a text box to appear only when a choice has been selected. Multiple selections can be made. Can someone provide me with a code for this?

Thanks!

6 replies

bstrahin
Level 6 ●●●●●●
Forum|alt.badge.img+38
  • Level 6 ●●●●●●
  • December 20, 2022

JohannesCE
Level 6 ●●●●●●
Forum|alt.badge.img+12
  • Level 6 ●●●●●●
  • June 27, 2023

Hi @isaibs, this one works for me:

 

Qualtrics.SurveyEngine.addOnload(function()
{
/*Place your JavaScript here to run when the page loads*/

});

Qualtrics.SurveyEngine.addOnReady(function() {
var text_box = this.getChoiceContainer().querySelector(".TextEntryBox");
jQuery(text_box).hide();

this.questionclick = function() {
var text_choice = jQuery(text_box).parent().children(".q-checked").length > 0;
if (text_choice) {
jQuery(text_box).show();
} else {
jQuery(text_box).hide();
}
};
});


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

});

 


TomG
Level 8 ●●●●●●●●
Forum|alt.badge.img+27
  • Level 8 ●●●●●●●●
  • June 27, 2023

Hi @isaibs, this one works for me:

 

Qualtrics.SurveyEngine.addOnload(function()
{
/*Place your JavaScript here to run when the page loads*/

});

Qualtrics.SurveyEngine.addOnReady(function() {
var text_box = this.getChoiceContainer().querySelector(".TextEntryBox");
jQuery(text_box).hide();

this.questionclick = function() {
var text_choice = jQuery(text_box).parent().children(".q-checked").length > 0;
if (text_choice) {
jQuery(text_box).show();
} else {
jQuery(text_box).hide();
}
};
});


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

});

 

There are some issues with this code such as not accounting for multiple allow text fields and the possibility of hidden entered text causing validation errors.


JohannesCE
Level 6 ●●●●●●
Forum|alt.badge.img+12
  • Level 6 ●●●●●●
  • June 27, 2023

Hi @TomG, I'm not sure why those two things should be necessary, but do you think that this one by @ahmedA resolves these issues?

Qualtrics.SurveyEngine.addOnload(function()
{
/*Place your JavaScript here to run when the page loads*/

});

Qualtrics.SurveyEngine.addOnReady(function () {
var text_box = this.getChoiceContainer().querySelector(".TextEntryBox");
text_box.hide();

this.questionclick = function () {
var text_choice = text_box.ancestors()[1].children[1].className.includes("checked");
if (text_choice) {
text_box.show();
} else {
text_box.hide();
}
};
});


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

});

 


TomG
Level 8 ●●●●●●●●
Forum|alt.badge.img+27
  • Level 8 ●●●●●●●●
  • June 27, 2023

@JohannesCE - no it doesn’t.


JohannesCE
Level 6 ●●●●●●
Forum|alt.badge.img+12
  • Level 6 ●●●●●●
  • June 27, 2023

Great