How to append additional zeros after the comma in a variable using javascript? | XM Community
Question

How to append additional zeros after the comma in a variable using javascript?

  • 25 January 2021
  • 8 replies
  • 45 views

Badge +1

Hello,
I would like to append additional zeros after the comma in a variable, in order to display it to participants. Since I have to many subcases to display, doing it by hand using the survey flow doesn´t seem feasible. I have tried to write a javascript code in which I recall my variable from the embedded data field, apply for example the "toFixed(x)" function, and save it back to embedded data. From what I understand, the problem of this approach is that, when qualtrics saves the variable from the javascript back to the embedded data, it considers it as a numerical variable and hence it´s unable to take into account the zeros that I´ve added (since mathematically they don´t make sense). Is there a way to add additional zeros after the comma of a variable and then display it? Or alternatively, is there a way to save a variable from javascript to qualtrics without using embedded data?
Thanks.


8 replies

Badge +1

Now it works. Thank you very much!

Userlevel 7
Badge +27

https://www.qualtrics.com/community/discussion/comment/33684#Comment_33684If the span is in the question text, you need to edit in "HTML View".

Badge +1

Ok, now the header seems to be loaded correctly!
the following piece of code seems to be still read as normal text:
${e://Field/payoff}
I wrote this in the block where I want my variable to be displayed. In the same block (in the javascript section) I wrote the other piece of code surrounding it with the "addOnLoad" function.

Userlevel 7
Badge +27

https://www.qualtrics.com/community/discussion/comment/33682#Comment_33682You weren't in source mode when you edited the header. You need to click the <> icon.

Badge +1

I went to "Look and Feel" --> "General" --> "Header" and edited it in the following way:

It doesn´t seem to work as qualtrics displays it as a normal text at the beginning of each block of the survey. Consequenty, also the following piece of code that I wrote in the javascript section (in the block where the variable with 2 decimals should be displayed) doesn´t seem to work:
Qualtrics.SurveyEngine.addOnload(function()
{
jQuery(".decimals3").each(function() {
   var numEl = jQuery(this);
   numEl.text(dec3formt.to(Number(numEl.text())));
});   
});
What am I doing wrong?
Thank you very much!

Userlevel 7
Badge +27

https://www.qualtrics.com/community/discussion/comment/33677#Comment_33677Add wNumb.js to your survey header. Go to jsdelivr.com find wNumb and jsdelivr will give you a


            

Scanning file for viruses.

Sorry, we're still checking this file's contents to make sure it's safe to download. Please try again in a few minutes.

OK

This file cannot be downloaded

Sorry, our virus scanner detected that this file isn't safe to download.

OK