How to set dependent sliders through Javascript | XM Community
Skip to main content

Hi!
I am trying to have three sliders in a question, where adjusting the first slider automatically adjusts the other two sliders with the following equation: If I change the value of the first slider to x, the second slider should update to 2x-x^2, and the third slider should update to 1 - X^2. I used a code that someone posted a year ago (https://community.qualtrics.com/XMcommunity/discussion/15518/how-to-set-the-value-on-a-slider-through-javascript#) that suppose to work, but for some reason, it isn't.
The code is:
var currentQuestionInfo = this.getQuestionInfo()
var postTag = currentQuestionInfo.postTag
var currentQuestionID = this.getQuestionInfo().QuestionID;
var Sli1Old = 50;
var Sli2Old = 75;
var Sli3Old = 75;
var respSli1 = document.getElementById(currentQuestionID + '~1~result');
var respSli2 = document.getElementById(currentQuestionID + '~2~result');
var respSli3 = document.getElementById(currentQuestionID + '~3~result');
    
Event.observe(document, 'mouseup',function(e){
   var currentResponse1 = respSli1.value;
    var currentResponse2 = respSli2.value;
    var currentResponse3 = respSli3.value;
    ( currentResponse1 != Sli1Old ) {
    if( currentResponse1 != Sli1Old ) {
    var forceAns2 = 2*currentResponse1 - currentResponse1^2;
var forceAns3 = 1 - currentResponse1^2;
document.getElementById("currentQuestionID~2~result").setValue(forceAns2);
document.getElementById("currentQuestionID~2~bar").style.width= forceAns2 + "px";
document.getElementById("currentQuestionID~3~result").setValue(forceAns3);
document.getElementById("currentQuestionID~3~bar").style.width= forceAns3 + "px";

    }
    var currentResponse1 = respSli1.value;
    var currentResponse2 = respSli2.value2
var currentResponse3 = respSli3.vlue;
    Sli1Old = currentResponse1;
Sli2Old = currentResponse2;
    Sli3Old = currentResponse3;
});

The code is almost identical to the code in the attached link (with a few adjustments), which also did not work (although it is mentioned there that it is, so maybe I did something wrong).
Thanks in advance!
Tom

    ( currentResponse1 != Sli1Old ) {
    if( currentResponse1 != Sli1Old ) {
1st line is probably a cut & paste error.
However, I can't see how the resulting values of those formulas could fit on 3 sliders sharing the same scale.


Leave a Reply