Convert a HTML input to a javascript variable | Experience Community
Skip to main content

Convert a HTML input to a javascript variable

  • Jan 4, 2023 3:54 PM UTC
  • 7 replies
  • 84 views

Forum|alt.badge.img+1

Dear XM community members,
Could you tell me how to convert a HTML input to a javascript variable. Indeed I need to collect the answer to a question and to use it in javascript.
Thank you

7 replies

TomG
Level 8 ●●●●●●●●
Forum|alt.badge.img+27
  • Level 8 ●●●●●●●●
  • Jan 4, 2023 4:18 PM UTC

Is the html input a Qualtrics question? If so, what question type?
Is the JavaScript on the same page as the input? Is it attached to the same question?


Forum|alt.badge.img+1
  • Author
  • Jan 4, 2023 4:20 PM UTC

Thanks Tom,
Yes the input is a Qualltrics question. The type is a text entry. The Javascript is attached to the same question


TomG
Level 8 ●●●●●●●●
Forum|alt.badge.img+27
  • Level 8 ●●●●●●●●
  • Jan 4, 2023 4:33 PM UTC

https://community.qualtrics.com/XMcommunity/discussion/comment/53620#Comment_53620It depends on when you want to capture the value.
On 'Next' click:
Qualtrics.SurveyEngine.addOnPageSubmit() {
var inputVal = jQuery("#"+this.questionId+" .InputText").val();
});
When input loses focus (i.e., input complete):
Qualtrics.SurveyEngine.addOnload() {
var inputVal;
jQuery("#"+this.questionId+" .InputText").blur(function() { inputVal = this.value; });
});
When characters are typed:
Qualtrics.SurveyEngine.addOnload() {
var inputVal;
jQuery("#"+this.questionId+" .InputText").on("input", function() { inputVal = this.value; });
});


Forum|alt.badge.img+1
  • Author
  • Jan 5, 2023 9:55 AM UTC

Thank you Tom. So participants should answer a qualtrics question where they have to write a number between 1 and 18 then in the second page, depending on the value of that number, A message will display the payment. I don't find any way to do it


TomG
Level 8 ●●●●●●●●
Forum|alt.badge.img+27
  • Level 8 ●●●●●●●●
  • Jan 5, 2023 2:31 PM UTC

https://community.qualtrics.com/XMcommunity/discussion/comment/53674#Comment_53674Is the payment 1 to 18 or some other number you are calculating?


Forum|alt.badge.img+1
  • Author
  • Jan 5, 2023 8:46 PM UTC

The Payment is other number I will compute depending on the value of the first number.


TomG
Level 8 ●●●●●●●●
Forum|alt.badge.img+27
  • Level 8 ●●●●●●●●
  • Jan 5, 2023 9:00 PM UTC

I would just do it with JS on the second page.
HTML where you want to show the payment:
{pipe answer from previous page here (i.e., 1 to 18)}
JS:
Qualtrics.SurveyEngine.addOnload(function() {
var payment = jQuery("#"+this.questionId+" .payment");
var paymentCalc = {calculation using Number(payment.text()) goes here};
payment.text(paymentCalc);
});