Custom JS: clickNextButton not working | XM Community
Skip to main content

Hi everyone!

Maybe someone can help me. I am trying to run a code on a page with 80 buttons that checks whether participants have selected all the buttons they are supposed to select. If they have, I want three things to happen:

  1. Participants get an alert telling them they did find all the buttons (works!)
  2. Percentage of corretly pressed buttons gets written in an embedded data field (does not work)
  3. Upon closing the alert, the Next-Button gets automatically pressed (does not work).

Any advice on how to make (2) and (3) happen would be greatly appreciated!

 

Here is the code I have so far:

Qualtrics.SurveyEngine.addOnload(function()
{
    this.hideNextButton();
    jQuery('.toggleable').on('click', function(){
           jQuery(this).toggleClass('chosen unchosen');
          var isChosen = jQuery(this).hasClass('chosen');
    });

    var correct = ;5, 6, 7, 13, 14, 33, 34, 41, 43, 49, 58, 59, 65, 79];
    var ncorr = 0;
    var wept_01 = 0;    
    
    jQuery('#check').on('click', function(){
        var wrongSelected = false;    
                
        jQuery('.chosen').each(function ()  {
                var chosenid = jQuery(this).attr('id');
                if (chosenid) {
                    chosenid = +chosenid;
                    if (jQuery.inArray(chosenid,correct) > -1){
                        ++ncorr;
                    }
                    else {
                    wrongSelected = true;
                    }
                }
        });

        if (wrongSelected) {
            alert ("There’s a mistake.");
            return false;
            }
        else if (ncorr < correct.length) {
            alert ("You missed some buttons.");
            return false;
            }
        else {
                var chosenPercentage = (ncorr / correct.length) * 100;
                alert("Congrats, you found them all!");
                return true;
                Qualtrics.SurveyEngine.setEmbeddedData("ncor_prac1", chosenPercentage);
                jQuery("#NextButton").click();
                }
        });
});

 

@LaDit

I believe the last else block has return true above the set embedded data and next button click statement, but return true ends the function, and the subsequent code won't be executed. You should move the Qualtrics.SurveyEngine.setEmbeddedData and jQuery("#NextButton").click(); lines above return true.

You can also use trigger to click: jQuery("#NextButton").trigger("click");

Hope this helps!


Hi Deepak!

Thank you for the hint - now triggering the next button works (though with a slight delay). However, setting the value of my embedded data filed to the value of chosenPercentage does not work - neither with setEmbeddedData nor with setJSEmbeddedData. Does anyone have any more suggestions regarding this issue?


And just to be clear: The embedded data field is defined at the beginning of the survey and exists both with and without the __js_  prefix. If I specify a number/string to write into the field, that works. But I need to write the value of the variable created in my JS to this field.

 


Sorry for the confusion. setJSEmbeddedData does indeed work, setEmbeddedData does not.


Leave a Reply