How to enable/disable the textbox depending on the choice of raido button | XM Community
Skip to main content

Hi.
I would like to control a text box based on the selection of radio buttons. The form will have 3 radio buttons and a text box. When the form is loaded, I want the text box to be disabled. However, when "Other" is selected in the radio buttons, I want the text box to be enabled. Additionally, if any option other than "Other" is selected, I want to clear the value of the text box and disable it again. Could you please tell me what code I should write to achieve this control?

 

@norikazukuriiwa You shouldn’t put “Other” type box in different question. Follow this post

How can I make the text entry box appear under the 'other please specify' option ONLY when it's clicked? | XM Community (qualtrics.com)


Hello Norikazu, you can leverage In-page display logic feature for this use case. Configure the text entry question to only appear if someone has selected “Other” in the above question.


 

Hello Norikazu, you can leverage In-page display logic feature for this use case. Configure the text entry question to only appear if someone has selected “Other” in the above question.

Thank you very much for your info.
As you pointed out, I implemented display logic and showed whole the project to my client but they asked me to reduce the page transitions as many as possible.
There’re more than 100 questions in the project and so many questions to hide depending on the answers.


@norikazukuriiwa If you want some custom JavaScript to show the city selection and the text entry on one page, you could add this JavaScript to the question which asks for the city: 

Qualtrics.SurveyEngine.addOnReady(function() {
var radioButton1 = document.getElementById('QR~' + this.questionId + '~1');
var radioButton2 = document.getElementById('QR~' + this.questionId + '~2');
var radioButtonOther = document.getElementById('QR~' + this.questionId + '~3');
var textBox = document.getElementById('QR~QID17');

// Disable the text box initially when the page loads
textBox.disabled = true;

// Function to handle radio button changes
function handleRadioChange() {
// If the selected radio button corresponds to "Other", enable the text box
if (radioButtonOther.checked) {
textBox.disabled = false;
} else {
// Otherwise, clear and disable the text box
textBox.value = '';
textBox.disabled = true;
}
}

// Attach event listeners to the radio buttons
radioButton1.addEventListener('change', handleRadioChange);
radioButton2.addEventListener('change', handleRadioChange);
radioButtonOther.addEventListener('change', handleRadioChange);
});

You would need to replace the QID17 ith the question ID of the text entry question. 

The logic expects “Other” to be the 3rd radio button.

Best
Christian


@norikazukuriiwa If you want some custom JavaScript to show the city selection and the text entry on one page, you could add this JavaScript to the question which asks for the city: 

Qualtrics.SurveyEngine.addOnReady(function() {
var radioButton1 = document.getElementById('QR~' + this.questionId + '~1');
var radioButton2 = document.getElementById('QR~' + this.questionId + '~2');
var radioButtonOther = document.getElementById('QR~' + this.questionId + '~3');
var textBox = document.getElementById('QR~QID17');

// Disable the text box initially when the page loads
textBox.disabled = true;

// Function to handle radio button changes
function handleRadioChange() {
// If the selected radio button corresponds to "Other", enable the text box
if (radioButtonOther.checked) {
textBox.disabled = false;
} else {
// Otherwise, clear and disable the text box
textBox.value = '';
textBox.disabled = true;
}
}

// Attach event listeners to the radio buttons
radioButton1.addEventListener('change', handleRadioChange);
radioButton2.addEventListener('change', handleRadioChange);
radioButtonOther.addEventListener('change', handleRadioChange);
});

You would need to replace the QID17 ith the question ID of the text entry question. 

The logic expects “Other” to be the 3rd radio button.

Best
Christian

This is exactly what I really wanted!!
This works perfect!!

Thank you very much for your kindness and I could release from the stress at all!


Leave a Reply