Math operation and Cleave function | XM Community
Solved

Math operation and Cleave function


Hi all,
I'm trying to ask the following question: "What is the chance that X will be more than 1.5*Q"?" where Q is the answer to a previous text box question. I use the expression $e{ q://QID166/ChoiceTextEntryValue * 1.5 } in place of 1.5*Q.
Unfortunately, being Q a large number, I used the "Cleave" function in the previous question to get thousand separators, so that I get 0 whenever Q is greater or equal than 1000.

Is there a way to fix this issue? I would really like to keep the thousand separators because Q is a very large number and I don't want survey participants to be confused. Ideally, I would like the thousand separator to show up also in the expression 1.5*Q
Many thanks!

icon

Best answer by TomG 12 May 2020, 17:14

View original

7 replies

Userlevel 7
Badge +27

You can use cleave's .getRawValue() to convert the value back to non-formatted number in the addOnPageSubmit function of the previous question. You can then use it in your Math Operation. You can then apply cleave to your 1.5*Q question to format that number.

Hi Tom,
Many thanks for your quick answer! I'm quite new to Qualtrics and Java, so I'm not sure how to implement your suggestion. In the previous question, I have the following code:
Qualtrics.SurveyEngine.addOnload(function()
{

});

Qualtrics.SurveyEngine.addOnReady(function()
{

});

Qualtrics.SurveyEngine.addOnload(function()
{

   var inputs = jQuery("input[type='text']");
inputs .toArray() .forEach(function(field)
{
new Cleave(field,
{
numeral: true,
numeralThousandsGroupStyle: 'thousand'
});
});


});

How should I modify it? And how would I apply the cleave function to the second question? Would it be the same code? Many thanks!

Userlevel 7
Badge +27

How should I modify it?

You don't need an array if you have a single text entry:
Qualtrics.SurveyEngine.addOnload(function() {
   var cleave = new Cleave(".InputText", {numeral: true, numeralThousandsGroupStyle: 'thousand'});
   Qualtrics.SurveyEngine.addOnPageSubmit(function() {
      cleave.element.value = cleave.getRawValue();
   });
});
And how would I apply the cleave function to the second question? Would it be the same code?

Yes, if it is an input.

Hi Tom,
Many thanks for the answer. I have coped your code in the Javascript section of the first question, but unfortunately I don't seem to be able to make it work. The expression $e{ q://QID166/ChoiceTextEntryValue * 1.5 } gives me a 0 when inserted as a choice and {Invalid Expression} when inserted in the question (which is where I would need it).

Userlevel 7
Badge +27

Is the first question a text entry question and the only one on the page? Is the second question on a different page (it has to be in order to pipe the answer from the first question)?

The first question is a text entry question. It is not the only question on the page but putting it on a separate page doesn't seem to make a difference. The second is on a different page and correctly references the piped text when not in a math operator or when it is lower than 1000. Otherwise, I just get 0 (the {Invalid Expression} doesn't indeed show up anymore). My code, in either questions, now looks like this:

Qualtrics.SurveyEngine.addOnload(function()

{


});


Qualtrics.SurveyEngine.addOnReady(function()

{


});


Qualtrics.SurveyEngine.addOnload(function()

{


  var inputs = jQuery("input[type='text']");

inputs .toArray() .forEach(function(field)

{

new Cleave(field,

{

numeral: true,

numeralThousandsGroupStyle: 'thousand'

});

});



});

Qualtrics.SurveyEngine.addOnload(function() {
  var cleave = new Cleave(".InputText", {numeral: true, numeralThousandsGroupStyle: 'thousand'});
  Qualtrics.SurveyEngine.addOnPageSubmit(function() {
     cleave.element.value = cleave.getRawValue();
  });
});

Userlevel 7
Badge +27

Two things:

  1. The new addOnload function replaces the old one. It isn't in addition to it.

  2. To post code, highlight the code, click the ¶ to the left, then " and Code Block.

Leave a Reply