Slider question with 2 statements. Javascript only applying to 1 statement | XM Community
Skip to main content
Question

Slider question with 2 statements. Javascript only applying to 1 statement


Forum|alt.badge.img+1

Hello, I hope someone here can help.

 

I am creating a slider style question in Qualtrics, that includes two statements within the question. The participant is required to provide a range of a total cost. Each statement (slider) represents a bound for the estimation. Slider 1 is the lowest bound, and Slider 2 is the highest bound for their cost estimate. I want the number in each statement to be converted to increments of 10,000, and have a $ added.

I have applied a mutation observer in JavaScript, which multiplies the result by 10,000, and add a $ symbol, to represent increments of $10,000 for each increment. The following JavaScript as follows was applied:

Qualtrics.SurveyEngine.addOnload(function()
{
/Place your JavaScript here to run when the page loads/
jQuery("ul.numbers").hide()

});

Qualtrics.SurveyEngine.addOnReady(function()
{
/Place your JavaScript here to run when the page is fully displayed/
jQuery("ul.numbers").hide()

// Select the node that will be observed for mutations
const targetNode = document.getElementsByClassName("sliderToolTipBox")[0];
let observer = new MutationObserver(function() {
targetNode.innerHTML = "$"+(Math.abs(targetNode.innerHTML)*10000);
observer.disconnect(); // turn observer off;
observer.observe(targetNode, {
attributes: true,
childList: true, // observe direct children
subtree: true, // and lower descendants too
characterDataOldValue: true // pass old data to callback
});// turn back on

});

// observe everything except attributes
observer.observe(targetNode, {
attributes: true,
childList: true, // observe direct children
subtree: true, // and lower descendants too
characterDataOldValue: true // pass old data to callback

});

});

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

});

However, this function only applies to the first slider, and would like to make it so it applies to both sliders. Secondly, I would like to make it so that a comma is added into each slider result, so that it differentiates thousands, hundeds of thousands etc ($10,000, $100,000, $1,000,000).

Please find attached a screenshot of the issue below:

For additional information, the slider question is set up so that it has 20 increments, and is NOT snapped. So it goes from 1-100. The first slider does convert to $10,000… etc, however the 2nd slider in the same question does not

 

Anyone know what I can do to fix it?

3 replies

TomG
Level 8 ●●●●●●●●
Forum|alt.badge.img+27
  • Level 8 ●●●●●●●●
  • 5909 replies
  • March 12, 2025

It only works on the first slider because document.getElementsByClassName("sliderToolTipBox")[0] only returns the tool tip box for the first slider.

Use Intl.NumberFormat() or toLocaleString() to format the number as currency with commas.


Forum|alt.badge.img+1

Thank you for your response.

I have tried making a Intl.NumberFormat() script, and it still is not working. This is what I have:

 

Qualtrics.SurveyEngine.addOnload(function() {
    var qid = this.getQuestionInfo().QuestionID;

    function formatCurrency(value) {
        return new Intl.NumberFormat('en-US', { style: 'currency', currency: 'USD', maximumFractionDigits: 0 }).format(value);
    }

    function roundToNearestTenThousand(value) {
        return Math.round(value / 10000) * 10000;
    }

    function updateSliderDisplay() {
        jQuery("#" + qid + " .q-slider-label-container").each(function() {
            var label = jQuery(this).find(".q-slider-label");
            var rawValue = label.text().replace(/[^0-9]/g, ""); // Extract numeric value
            if (rawValue) {
                var roundedValue = roundToNearestTenThousand(parseInt(rawValue, 10));
                label.text(formatCurrency(roundedValue));
            }
        });
    }

    /* Monitor changes on slider */
    jQuery("#" + qid + " .q-slider-container").on("mouseup touchend input", function() {
        updateSliderDisplay();
    });

    /* Apply formatting on page load */
    updateSliderDisplay();
});

Qualtrics.SurveyEngine.addOnReady(function() {
    setTimeout(updateSliderDisplay, 500); // Ensure formatting applies after page fully loads
});

Qualtrics.SurveyEngine.addOnUnload(function() {
    /* Clean up if necessary */
});

 

Am I missing something, or have I gotten it wrong?


TomG
Level 8 ●●●●●●●●
Forum|alt.badge.img+27
  • Level 8 ●●●●●●●●
  • 5909 replies
  • March 13, 2025

Try setting the currency format immediately after you set qid:

var cf = new Intl.NumberFormat('en-US', { style: 'currency', currency: 'USD', maximumFractionDigits: 0 });

Then format the label:

 label.text(cf.format(roundedValue));

You don’t need the formatCurrency function.

A Mutation Observer is a better way to do it. Not sure why you gave up on it.


Leave a Reply