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

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

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?


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


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


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


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


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


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


Leave a Reply