Using JQuery change function for many questions | XM Community
Question

Using JQuery change function for many questions

  • 16 July 2019
  • 8 replies
  • 25 views

Userlevel 1
Badge +2
I'm new to Qualtrics and fairly new to Javascript so hope I can make this intelligible.

So this simple bit code changes the value of one box depending on the value of another. (I'm sure it can be done more elegantly.)

My question is: how can I repeat this code for many questions without having to write a separate function for each?

Thanks.

`Qualtrics.SurveyEngine.addOnReady(function()
{
/*Place your JavaScript here to run when the page is fully displayed*/
var el1="QR~" + this.questionId + "~1~1~TEXT"; //box 1,1
var el1a=jQuery("[id='" + el1 + "']");
var el2="QR~" + this.questionId + "~1~2~TEXT"; //box 1,2
var el2a=jQuery("[id='" + el2 + "']");

el1a.change(function() {
if (el1a.val().length>0){
if (isNaN(el1a.val()))

{jQuery("[id='" + el2 + "']").val("Not a number");}

else

{jQuery("[id='" + el2 + "']").val("A number");}}
})

});`

8 replies

Userlevel 7
Badge +6
Do you mind if I ask what you are trying to do exactly? The answer may be easier solved in Qualtrics, without additional coding (which is helpful because then Q support will assist if something doesn't work as expected).
Userlevel 1
Badge +2
Thanks for your response. Yes, I should have explained.

I have a survey involving many boxes into which respondents will enter numbers and I need to do a certain amount of validation. For example, some questions will have three boxes and I need to check the value in the third box is less than or equal to the sum of the first two (in others there might be 4 boxes which need checking).

Does that clarify? If there is an easier approach, I'm all ears.
Userlevel 7
Badge +6
You can accomplish this without JavaScript.

Basically you need to use Custom Validation and Math Operations to check if the value in box Z is less than or equal to the values of X + Y.

Math Operations can be a little tricky, but your use case is pretty straight forward.
Link too Math Operations: https://www.qualtrics.com/support/survey-platform/survey-module/editing-questions/piped-text/math-operations/
Userlevel 7
Badge +6
@StephenR - I am happy to explain how this would work, step-by-step, if you are unfamiliar with Custom Validation and Math Operations.
Userlevel 1
Badge +2
@Akdashboard - thanks very much for that. I'll check it out in the next day or two and let you know if I get stuck.
Userlevel 1
Badge +2
@Akdashboard - your offer of help would be welcome.

My question is a matrix table with text entry boxes. It has 4 columns and 3 rows and for each column I want to check the top box + middle box <= bottom box.

The problem I face now is that I already have some custom validation on this question and there doesn't seem to be a way to add a second type of custom validation?
Userlevel 7
Badge +6
@StephenR - DM me the the .qsf and give me an example question and logic you are trying to do. I will try to take a look at it today, time allowing. I can't say exactly what you need to do until I know what I am looking at.
Userlevel 1
Badge +2
@Akdashboard - thanks, have attached a QSF file here, hope that's what you meant.

So only one question, eg. for Q1 the validation needs to check that A+B<=C (obviously then just the same for Q2, Q3 and Q4 columns).

Possible snag is that I have already added custom validation to check what's entered in the boxes (using regular expressions).

Do shout if anything is unclear.

Leave a Reply