Ratio of answers to two questions | XM Community
Skip to main content
I must be missing something blindingly obvious but my JS skills are slight.



I have question 1 with a single text entry box.



Question 2 is side-by-side with two columns of open text boxes. Once the respondent has done Q1 and entered something in the top left box of Q2 I want the ratio of the two to appear in the top box of column 2 in Q2.



For some reason the Q1 value is not being transferred.



Qualtrics.SurveyEngine.addOnReady(function()

{

/*Place your JavaScript here to run when the page is fully displayed*/

inputW = jQuery('[name="QR~QID54#1~1~1~TEXT"]'); //question 2 first box

inputEmp =jQuery('[name="QR~QID14~1"]'); //question 1

inputWpc=jQuery('[name="QR~QID54#2~1~1~TEXT"]'); //box to show ratio



inputW.change(function ()

{

var val1 = (isNaN(parseInt(inputW.val()))) ? 0 : parseInt(inputW.val());

console.log(val1);

var val2 = (isNaN(parseInt(inputEmp.val()))) ? 0 : parseInt(inputEmp.val());

console.log(val2); //just shows 0

var val3= (100*val1/val2).toFixed(1)+"%";

console.log(val3);

if (inputW.val().length>0 && inputEmp.val().length>0)

{inputWpc.val(val3);}

else

{inputWpc.val("N/A");}

});

});



I've tried putting the variable in the footer to no avail.



Thanks for any pointers.
Hey Stephen,

I created a quick sandbox environment and plugged your code in and it worked great for me. I'm exporting my survey and attaching it here just to let you know your code doesn't seem to be the problem. I know the generic "works great on my machine" isn't what you want to hear, but sometimes it's helpful to know maybe you're looking in the wrong place for the fix. Here's my tweaks to your code. I only had to modify the jQuery search strings.



`Qualtrics.SurveyEngine.addOnReady(function()

{

/*Place your JavaScript here to run when the page is fully displayed*/

inputW = jQuery('[name="QR~QID2#1~1~1~TEXT"]'); //question 2 first box

inputEmp =jQuery('[name="QR~QID1~TEXT"]'); //question 1

inputWpc=jQuery('[name="QR~QID2#2~1~1~TEXT"]'); //box to show ratio



inputW.change(function ()

{

var val1 = (isNaN(parseInt(inputW.val()))) ? 0 : parseInt(inputW.val());

console.log(val1);

var val2 = (isNaN(parseInt(inputEmp.val()))) ? 0 : parseInt(inputEmp.val());

console.log(val2); //just shows 0

var val3= (100*val1/val2).toFixed(1)+"%";

console.log(val3);

if (inputW.val().length>0 && inputEmp.val().length>0)

{inputWpc.val(val3);}

else

{inputWpc.val("N/A");}

});

});`



(I can never get the code block to format correctly, please forgive 😉 )

I might throw out too that your Q1 value is QR~QID14~1, but if that value really is a textbox, it should have ~TEXT on the end, e.i. my Q1 ID is QR~QID1~TEXT, so you may check your jQuery string on Q1.
Thank you so much Jeremy.



I can't believe I missed that "TEXT" bit. I haven't yet worked out the logic or found a reference for how to refer to different sorts of questions so was mostly winging out. For most of the questions the ID and Name was the same, but I had missed that that wasn't the case for that question.



As a follow-on, if it's not too cheeky, do you know if it's possible to trigger the calculation once numbers have been entered? As it stands you have to click somewhere or tab.



Thanks again.
No problem, man. Sometimes you just need a second set of eyes 😉



And check out using the KeyUp event instead of the change event. Should be a really easy change in the code; makes the event fire on key up, so you should see the ratio field in Q2, column 2 dynamically change as they type in Q2, column 1.
Great, thanks again Jeremy.

Leave a Reply