Automatically calculate percentage within a Survey from two price fields within the same question | XM Community
Solved

Automatically calculate percentage within a Survey from two price fields within the same question

  • 17 July 2018
  • 1 reply
  • 45 views

Is it possible within a Survey question to have three fields: Current Price, Sale Price, and Percentage off but have the Percentage off field be populated in real time from the current price and sale price inputs? A Qualtrics support agent stated it may be possible with Javascript but I have limited experience with Javascript and want to ensure it is possible?
icon

Best answer by Samarth 17 July 2018, 11:56

View original

1 reply

Userlevel 4
Badge +5
Hi @bonjessi,

I have calculated the discount % in attached QSF file using Javascript. Below is the Code.

var $jq= jQuery.noConflict();
$jq('.SumInput:last').attr('readonly',true);
$jq('.SumInput').keyup(function(){
$jq('.SumInput:last').val(calc());
});


function calc(){ var disc = 0;
var cp =getVal(parseInt($jq('.SumInput').eq(0).val()));
var sp =getVal(parseInt($jq('.SumInput').eq(1).val()));
if (cp>sp && sp>0)
{
disc = Math.round(((cp - sp)/cp)*100,2);
}
return disc; };

function getVal(val){ var fval=0;
if (isNaN(parseInt(val)))
{ fval = 0; } else { fval = val; }
return fval;
}


Hope this works for you!

Regard,
Samarth

Leave a Reply