How to adjust Javascript Code for Constant Sum Question of Bar Type instead of Choices Type? | XM Community
Skip to main content
Hi,

I would like participants in a study to allocate 100 points between two participants (A and 😎. Points are converted into cents at different rates.



With help, I now have a javascript code that works when the constant sum question is of Choices type.



To make the question more intuitive, I'd like to change it to Bars type. However, all the formatting I had with the JS code gets messed up.



Can you give me advice on how to change my JS code for a constant sum question of Bars type?



Here is my code now:

Qualtrics.SurveyEngine.addOnload(function()

{

var inputs = $(this.getQuestionContainer()).select('input[type="text"]');





$(inputs[0]).insert({after: '<div id="input1" style="display: inline-block; width: 600px; \\

text-align: center, left; font-family:arial,helvetica,sans-serif;"> \\

  \\

<span style="font-size:14px;"><span style="font-family:arial,helvetica,sans-serif;">\\

<strong><span style="color:#0000CD;"></span> points for <u>PARTICIPANT A</u> worth 1 cent each. \\

She gets 0 cents. \\

<span style="color:#B22222;"></span>\\

</strong></span></span><span style="font-family: arial, helvetica, sans-serif; font-size: 14px;"> </div>'});

$(inputs[1]).insert({after: '<div id="input2" style="display: inline-block; width: 600px; \\

text-align: left; font-family:arial,helvetica,sans-serif;"> \\

  \\

<span style="font-size:14px;"><span style="font-family:arial,helvetica,sans-serif;">\\

<strong><span style="color:#800080;"></span> points for <u> PARTICIPANT B</u> worth 2 cents each. \\

She gets 0 cents. <span style="color:#800080;"></span>\\

</strong></span></span><span style="font-family: arial, helvetica, sans-serif; font-size: 14px;">\\

</div>'});

$(inputs[2]).insert({after: '<span style="display: inline-block; width: 600px;"></span>'});



$('QR~QID8~1').on('input', function () {

input0 = $('QR~QID8~1').value;

var inputsum0 = parseInt(input0);

$('input1').innerHTML = '';

if ($('QR~QID8~1').value == '') {

$('input1').insert('   <span style="font-size:14px;"><span style="font-family:arial,helvetica,sans-serif;">\\

<strong><span style="color:#0000CD;"></span> points for <u>PARTICIPANT A</u> worth 1 cent each. \\

She gets 0 cents. <span style="color:#B22222;"></span>\\

</strong></span></span><span style="font-family: arial, helvetica, sans-serif; font-size: 14px;"> ' + ' ');

} else {

$('input1').insert('   <span style="font-size:14px;"><span style="font-family:arial,helvetica,sans-serif;">\\

<strong><span style="color:#0000CD;"></span> points for <u>PARTICIPANT A</u> worth 1 cent each. \\

She gets </span> \\

</span></span><span style="font-family: arial, helvetica, sans-serif; font-size: 14px;"><u>\\

' + (inputsum0 * 1).toFixed(0) + '</u> <span style="font-size:14px;"><span style="font-family:arial,helvetica,sans-serif;">\\

<span style="color:#B22222;"></span> cents. <span style="color:#B22222;"></span>\\

</strong></span></span><span style="font-family: arial, helvetica, sans-serif; font-size: 14px;">\\

');

}

})



$('QR~QID8~2').on('input', function () {

input1 = $('QR~QID8~2').value;

var inputsum1 = parseInt(input1);

$('input2').innerHTML = '';

if ($('QR~QID8~2').value == '') {

$('input2').insert('   <span style="font-size:14px;"><span style="font-family:arial,helvetica,sans-serif;">\\

<strong><span style="color:#800080;"></span> points for <u> PARTICIPANT B</u> worth 2 cents each. \\

She gets 0 cents. <span style="color:#800080;"></span>\\

</strong></span></span><span style="font-family: arial, helvetica, sans-serif; font-size: 14px;">\\

' + ' ');

} else {

$('input2').insert('   <span style="font-size:14px;"><span style="font-family:arial,helvetica,sans-serif;">\\

<strong><span style="color:#800080;"></span> points for <u> PARTICIPANT B</u> worth 2 cents each. \\

She gets </span>\\

</span></span><span style="font-family: arial, helvetica, sans-serif; font-size: 14px;"><u>\\

' + (inputsum1 * 2).toFixed(0) + '</u> <span style="font-size:14px;"><span style="font-family:arial,helvetica,sans-serif;">\\

<span style="color:#800080;"></span> cents. <span style="color:#800080;"></span>\\

</strong></span></span><span style="font-family: arial, helvetica, sans-serif; font-size: 14px;">\\

');

}

})







});



Here are two images of how the question looks like (with Choices type and with Bars type)



!

!



Any feedback would be really helpful! Thanks!
The code is to big to check out on the go you can contact @Shashi he is quite good at CustomCode 🙂 .
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;

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(document).on('change', function(){



var A=parseInt(jQuery("[id='"+that+"~1~result']").val());

jQuery("#currA").text(A);

jQuery("#totalA").text(A);

var B=parseInt(jQuery("[id='"+that+"~2~result']").val());

jQuery("#currB").text(B);

jQuery("#totalB").text(B*2);



});



Output:



!



If you want any further customization, feel free to post.

Leave a Reply