Math operation and Cleave function | Experience Community
Skip to main content
Solved

Math operation and Cleave function

  • May 11, 2020 8:01 PM UTC
  • 7 replies
  • 93 views

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!

Best answer by TomG

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.

View original

7 replies

TomG
Level 8 ●●●●●●●●
Forum|alt.badge.img+27
  • Level 8 ●●●●●●●●
  • May 11, 2020 8:12 PM UTC

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.


  • Author
  • May 11, 2020 8:26 PM UTC

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!


TomG
Level 8 ●●●●●●●●
Forum|alt.badge.img+27
  • Level 8 ●●●●●●●●
  • May 12, 2020 12:34 PM UTC

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.


  • Author
  • May 12, 2020 1:48 PM UTC

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).


TomG
Level 8 ●●●●●●●●
Forum|alt.badge.img+27
  • Level 8 ●●●●●●●●
  • May 12, 2020 2:28 PM UTC

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)?


  • Author
  • May 12, 2020 3:06 PM UTC

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();
  });
});


TomG
Level 8 ●●●●●●●●
Forum|alt.badge.img+27
  • Level 8 ●●●●●●●●
  • Answer
  • May 12, 2020 3:14 PM UTC

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.