Solved
How to dynamically adjust text & numbers below sliders in constant sum question?
Hi everyone,
For a research study, I would like participants to allocate 100 tokens among two recipients. Each token is worth $5.
In a simple version of the question, I managed to have a javascript code, such that when participants entered the token allocations (e.g., 50/50), they would automatically see the implied $ amount ($250/$250).
To make the question more appealing and intuitive, I would like participants to move slider bars indicating the token allocations (instead of entering the desired amounts). While participants adjust the sliders to decide how many tokens to allocate, I would like the text on the screen to also adjust and display the implied $ amount.
Searching online I found this link (https://stackoverflow.com/questions/29538632/in-qualtrics-how-to-dynamically-connect-two-sliders) to add two connected sliders to my question.
I've added it to my question. The problem is that I can't manage to change the javascript code such that, when the participant moves the sliders, the text on the screen also adjusts the implied $ amount.
I have little knowledge of javascript, and would very much appreciate your input. How can I have the question text change as the sliders are moved?
Here is a link to my survey question (code), which combines the code generating the two connected sliders, with my previous code adjusting the text when token amounts were entered directly.
https://drive.google.com/open?id=1SII-1-O_HT_yBNNR-6TvnzmDjMZTzvR4
Thank you very much!
Best answer by Anonymous
Hello @Marta ,
The following code will work for "Constant sum" -> "Draggable Bars", with choices= 2.
Paste the following code in the `js(OnReady)` of the Constant sum question.
var that=this.questionId;
var p= Math.round(jQuery("[id='"+that+"~1~track']").width()/100);
jQuery("<p style='width:100%'><span id='currA'>0</span> points for <u>PARTICIPANT A</u> worth 1 cent each. She gets <u><span id='totalA'>0</span></u> cents</p>").insertBefore("tr.First");
jQuery("<p style='width:100%'><span id='currB'>0</span> points for <u>PARTICIPANT B</u> worth 2 cent each. She gets <u><span id='totalB'>0</span></u> cents</p>").insertAfter("tr.First");
jQuery("[id='"+that+"~1~result']").bind('input propertychange',function(){
var A=parseInt(jQuery("[id='"+that+"~1~result']").val());
console.log(A);
jQuery("[id='"+that+"~2~result']").val(100-A);
var t=parseInt(100-A);
jQuery("[id='"+that+"~2~holder']").addClass("activated");
jQuery("[id='"+that+"~2~handle']").css({"left":p*t+"px"});
jQuery("[id='"+that+"~2~bar']").css({"width":p*t+"px"});
var A=parseInt(jQuery("[id='"+that+"~1~result']").val());
jQuery("#currA").text(A);
jQuery("#totalA").text(A*5);
var B=parseInt(jQuery("[id='"+that+"~2~result']").val());
jQuery("#currB").text(B);
jQuery("#totalB").text(B*5);
});
jQuery("[id='"+that+"~2~result']").bind('input propertychange',function(){
var B=parseInt(jQuery("[id='"+that+"~2~result']").val());
console.log(B);
jQuery("[id='"+that+"~1~result']").val(100-B);
var t=parseInt(100-B);
jQuery("[id='"+that+"~1~holder']").addClass("activated");
jQuery("[id='"+that+"~1~handle']").css({"left":p*t+"px"});
jQuery("[id='"+that+"~1~bar']").css({"width":p*t+"px"});
var A=parseInt(jQuery("[id='"+that+"~1~result']").val());
jQuery("#currA").text(A);
jQuery("#totalA").text(A*5);
var B=parseInt(jQuery("[id='"+that+"~2~result']").val());
jQuery("#currB").text(B);
jQuery("#totalB").text(B*5);
});
jQuery(document).on('change', function(){
var A=parseInt(jQuery("[id='"+that+"~1~result']").val());
jQuery("#currA").text(A);
jQuery("#totalA").text(A*5);
var B=parseInt(jQuery("[id='"+that+"~2~result']").val());
jQuery("#currB").text(B);
jQuery("#totalB").text(B*5);
});
Please find the attached QSF file of implementation.
Preview Link
Enter your E-mail address. We'll send you an e-mail with instructions to reset your password.

Also change the start text in the JS code accordingly
Here's the code:
Qualtrics.SurveyEngine.addOnReady(function()
{
/*Place your JavaScript here to run when the page is fully displayed*/
var that=this.questionId;
jQuery("[id='"+that+"~2~holder']").addClass("activated");
jQuery("[id='"+that+"~1~holder']").addClass("activated");
var p= Math.round(jQuery("[id='"+that+"~1~track']").width()/100);
jQuery("<p id='customid' style='width:100%; margin-bottom:10px; text-align: center'> \\
<br> <br> <strong> \\
After worker 1 works TODAY, <br> she/he </strong> keeps <span id='currA'>100</span> tokens, \\
worth $0.10 each. She gets <strong><u>$<span id='totalA'>10</span></u> \\
TODAY.</strong></p>").insertAfter("div.ChoiceStructure");
jQuery("<p style='width:100%; text-align: center'> \\
<br> <strong> \\
After worker 2 works IN 1 MONTH, <br> she/he </strong> gets <span id='currB'>0</span> tokens, \\
worth $0.08 each. She gets <strong><u>$<span id='totalB'>0</span></u> \\
in 1 MONTH.</strong></p>").insertAfter("#customid");
jQuery("[id='"+that+"~1~result']").bind('input propertychange',function(){
var A=parseFloat(jQuery("[id='"+that+"~1~result']").val());
console.log(A);
if((A+parseInt(jQuery("[id='"+that+"~2~result']").val()))==100) {
Qualtrics.SurveyEngine.setEmbeddedData( 'SliderValue1_1', A );
Qualtrics.SurveyEngine.setEmbeddedData( 'SliderValue1_2',parseInt(jQuery("[id='"+that+"~2~result']").val()));
console.log("rturn");
return;
}
jQuery("[id='"+that+"~2~result']").val(100-A);
var t=parseFloat(100-A);
jQuery("[id='"+that+"~2~holder']").addClass("activated");
jQuery("[id='"+that+"~2~handle']").css({"left":p*t+"px"});
jQuery("[id='"+that+"~2~bar']").css({"width":p*t+"px"});
var A=parseFloat(jQuery("[id='"+that+"~1~result']").val());
jQuery("#currA").text(A);
jQuery("#totalA").text((A*0.1).toFixed(2));
var B=parseFloat(jQuery("[id='"+that+"~2~result']").val());
jQuery("#currB").text(B);
jQuery("#totalB").text((B*0.08).toFixed(2));
});
jQuery("[id='"+that+"~2~result']").bind('input propertychange',function(){
var B=parseFloat(jQuery("[id='"+that+"~2~result']").val());
console.log(B);
if((B+parseInt(jQuery("[id='"+that+"~1~result']").val()))==100) {
Qualtrics.SurveyEngine.setEmbeddedData( 'SliderValue1_2', B);
Qualtrics.SurveyEngine.setEmbeddedData( 'SliderValue1_1',parseInt(jQuery("[id='"+that+"~1~result']").val()));
console.log("rturn");
return;
}
jQuery("[id='"+that+"~1~result']").val(100-B);
var t=parseFloat(100-B);
jQuery("[id='"+that+"~1~holder']").addClass("activated");
jQuery("[id='"+that+"~1~handle']").css({"left":p*t+"px"});
jQuery("[id='"+that+"~1~bar']").css({"width":p*t+"px"});
var A=parseFloat(jQuery("[id='"+that+"~1~result']").val());
jQuery("#currA").text(A);
jQuery("#totalA").text((A*0.1).toFixed(2));
var B=parseFloat(jQuery("[id='"+that+"~2~result']").val());
jQuery("#currB").text(B);
jQuery("#totalB").text((B*0.08).toFixed(2));
});
jQuery(document).on('change', function(){
var A=parseFloat(jQuery("[id='"+that+"~1~result']").val());
jQuery("#currA").text(A);
jQuery("#totalA").text((A*0.1).toFixed(2));
var B=parseFloat(jQuery("[id='"+that+"~2~result']").val());
jQuery("#currB").text(B);
jQuery("#totalB").text((B*0.08).toFixed(2));
});
});