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
View originalLeave a Reply
Enter your E-mail address. We'll send you an e-mail with instructions to reset your password.