Solved
Ratio of answers to two questions
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.
Best answer by JeremyK
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.
Enter your E-mail address. We'll send you an e-mail with instructions to reset your password.
