Search code examples
javascriptbuttonqualtrics

Qualtrics-Javascript code: Next button is hidden only in one page


For 30 Qualtrics pages, I'm using the code below (which I borrowed from here) to for each page:

  1. hide the "next" button
  2. give two choices to select from by using a "keystroke"
  3. once the choice is made, automatically go to the next page (where the same routine would be followed)

Qualtrics.SurveyEngine.addOnload(function()
{
    $('Buttons').hide();
    if(window.location.pathname.match(/^\/jfe[0-9]?\/preview/)) {
        $(this.questionId).select('input').first().focus();
    }   
    var that = this;

    Event.observe(document, 'keydown', function keydownCallback(e) {
        var choiceID = null;

        switch (e.keyCode) {
            case 74: // 'j' was pressed
                choiceID = 1;
                break;
            case 75: // 'k' was pressed
                choiceID = 2;
                break;
        }

        if (choiceID) {
            Event.stopObserving(document, 'keydown', keydownCallback);
            that.setChoiceValue(choiceID, true);
            $('NextButton').click();
        }
    });
});

To the best of my knowledge this code has been used by other users.

The problem however is that the "Next" button is hidden only on the first page. From the second page onward the "Next" button is always displayed although the rest of the code works fine.

Any idea where the problem is?

Best,


Solution

  • It's a timing issue due to changes in Qualtrics since that post. Change addOnload to addOnReady.