Timothy Huerta
Timothy Huerta

Reputation: 11

Evaluating a condition in qualtrics once the next button is clicked

I was hoping someone might have some insight you could offer on a problem I have been facing with Qualtrics. Simply, I want Qualtrics to evaluate the contents of a numeric field (i.e. a medical record number, take the five right characters and then compare it against an array of random numbers and return a value of 1 if it is in the list and 0 if it is not in the list). It seems like it should be easy, but because Qualtrics offers no support on the use of Javascript, when the code I prepared didn’t work, I had no one I could call about it… then I found your a post on StackOverflow and thought I’d ask.

I based my answer on the solution found at: In Qualtrics Surveys, how can you save the response to an item into embedded data when the next button is clicked?

It is a year old and I was thinking my solution may have failed because the implementation Qualtrics is using doesn’t support this anymore. Who knows.

I created a simplified version:

Qualtrics.SurveyEngine.addOnload(function()
{var currentQuestionID = this.getQuestionInfo().QuestionID;
    var resultEmbeddedName = "MRN_RND";
    $('NextButton').onclick = function (event) {
        var responseTextField = document.getElementById(currentQuestionID);
        var randomizercheck = responseTextField.value;
        Qualtrics.SurveyEngine.setEmbeddedData(resultEmbeddedName, randomizercheck);
        Qualtrics.SurveyEngine.navClick(event, 'NextButton');
};
});

I thought I’d start with something simple and once I am sure it works I can make it more complex. Can’t even get this to work, so I can’t tweak it to do what I need. Again, this is only a waypoint on the way to the solution I was hoping for, but I'm trying to solve the problem incrementally so I can understand where I've gone wrong.

Upvotes: 1

Views: 1699

Answers (2)

T. Gibbons
T. Gibbons

Reputation: 5004

The main problem is that responseTextField isn't the input field. Also, I think you probably want to trigger the event when the text input changes, not when the Next button is clicked. Try this:

Qualtrics.SurveyEngine.addOnload(function()
{
    var currentQuestionID = this.questionId;
    var resultEmbeddedName = "MRN_RND";
    var responseTextField = $(currentQuestionID).select('input.InputText').first();
    responseTextField.observe("keyup", function (event) {
        var randomizercheck = responseTextField.value;
        Qualtrics.SurveyEngine.setEmbeddedData(resultEmbeddedName, randomizercheck);
    };
});

Note: You must define the embedded variable MRN_RND in the survey flow before executing the block with the text input question.

Upvotes: 1

Shayou
Shayou

Reputation: 44

This is a hack, not the best solution. Try replacing:

var currentQuestionID = this.getQuestionInfo().QuestionID;

With:

var currentQuestionID = Object.keys(Qualtrics.SurveyEngine.QuestionInfo)[0];

This is assuming you do not have multiple questions in the page.

Upvotes: 1

Related Questions