Hi! I want a Graphic Slider with labels under it so that respondents can see the options and a “Don't know / N/A” checkbox that makes the question null / excludes it from analysis. I can't get the checkbox to work. This is what I have done so far:
HTML in the question:
<div style="text-align: left;" class="question-text"><b>Graphic Slider with default images:</b></div>
<label style="display: block; text-align: center; margin-top: 10px;" id="checkboxLabel"> <input type="checkbox" id="excludeOption"> Weet ik niet </label>
JS applied to the question:
Qualtrics.SurveyEngine.addOnload(function() { /*Place your JavaScript here to run when the page loads*/
});
Qualtrics.SurveyEngine.addOnReady(function() { var sliderContainer = document.querySelector('.BarContainer'); var labelsContainer = document.querySelector('.labels-container'); var checkboxLabel = document.querySelector('#checkboxLabel');
Qualtrics.SurveyEngine.addOnUnload(function() { /*Place your JavaScript here to run when the page is unloaded*/
});
Result:
How can I make the N/A option to work? And why is the text (or slider) not centered?
Page 1 / 1
Hi @JohannesCE ,
To make the "N/A" option work and center the text (or slider), you'll need to make some adjustments to your HTML and JavaScript code.
Centering the Text (or Slider): To center the text (or slider), you can apply some CSS styles to the elements. By adding text-align: center to the container and slider, you ensure that the text and slider are centered. Add the following styles to your HTML elements:
<div style="text-align: center;" class="question-text"><b>Graphic Slider with default images:</b></div>
<div style="display: flex; justify-content: space-between; width: 100%; margin-top: 10px; text-align: center;" class="labels-container"> <!-- Your label spans here --> </div>
<label style="display: block; text-align: center; margin-top: 10px;" id="checkboxLabel"> <!-- Your checkbox input here --> </label>
To make the "N/A" option work, you need to set the slider's value to null when the checkbox is checked. Additionally, you need to handle the case when the checkbox is unchecked to re-enable the slider. Try to modify your JavaScript as below
Qualtrics.SurveyEngine.addOnReady(function() { var sliderContainer = document.querySelector('#sliderContainer'); var labelsContainer = document.querySelector('.labels-container'); var checkboxLabel = document.querySelector('#checkboxLabel');
var checkbox = document.querySelector('#excludeOption'); var slider = document.querySelector('.q-slider'); var that = this;
// Function to disable/enable the slider based on checkbox state function toggleSliderState() { if (checkbox.checked) { slider.disabled = true; that.setChoiceValue(1, null); // Set the choice value to null (N/A) } else { slider.disabled = false; } }
// Event listener for the checkbox checkbox.addEventListener('change', function() { toggleSliderState(); });
// Check the initial state of the checkbox toggleSliderState(); });
Hi @Nanditha MM , thanks! However, your code is not working for me. This is what I get:
I made some adjustments to the JV based on your suggestions, but
Qualtrics.SurveyEngine.addOnReady(function() { var sliderContainer = document.querySelector('#sliderContainer'); var labelsContainer = document.querySelector('.labels-container'); var checkboxLabel = document.querySelector('#checkboxLabel');
var slider = this.getQuestionContainer().querySelector('.horizontalbar');
var checkbox = document.querySelector('#excludeOption'); var that = this;
// Function to disable/enable the slider based on checkbox state function toggleSliderState() { if (checkbox.checked) { slider.disabled = true; that.setChoiceValue(1, null); // Set the choice value to null (N/A) } else { slider.disabled = false; } }
// Event listener for the checkbox checkbox.addEventListener('change', function() { toggleSliderState(); });
// Check the initial state of the checkbox toggleSliderState(); });
Qualtrics.SurveyEngine.addOnload(function() { /*Place your JavaScript here to run when the page loads*/ });
Qualtrics.SurveyEngine.addOnUnload(function() { /*Place your JavaScript here to run when the page is unloaded*/ });
The adjusted HTML:
<div style="text-align: left;" class="question-text"> <b>Graphic Slider with default images:</b> </div>
<label id="checkboxLabel" style="display: block; text-align: center; margin-top: 10px;"> <input type="checkbox" id="excludeOption"> Weet ik niet </label>
The result:
Still no checkbox!
Hi @JohannesCE , please try with the below code once:
Qualtrics.SurveyEngine.addOnReady(function() { var sliderContainer = document.querySelector('#sliderContainer'); var labelsContainer = document.querySelector('.labels-container'); var checkboxLabel = document.querySelector('#checkboxLabel');
var slider = this.getQuestionContainer().querySelector('.q-slider-container');
var checkbox = document.querySelector('#excludeOption'); var that = this;
// Function to disable/enable the slider based on checkbox state function toggleSliderState() { if (checkbox.checked) { slider.style.pointerEvents = 'none'; // Disable interactions slider.style.opacity = '0.5'; // Reduce opacity for visual indication that.setChoiceValue(1, null); // Set the choice value to null (N/A) } else { slider.style.pointerEvents = 'auto'; // Enable interactions slider.style.opacity = '1'; // Restore opacity } }
// Event listener for the checkbox checkbox.addEventListener('change', function() { toggleSliderState(); });
// Check the initial state of the checkbox toggleSliderState(); });
and the css code to your survey to improve the appearance of the disabled slider:
The checkbox needs some CSS to override the Qualtrics styling that is hiding it. Also, adjusting the margins of the slider track seems to work okay to center it beneath the Smiley face. Try updating your HTML with the below, where the CSS is included. Update with your questions QID: