Hiding the "next" button until a key combination is typed a certain number of times | XM Community
Skip to main content
Hello!



I had some JavaScript which used to work for hiding the next button until a survey taker had typed "a" then "b" 100 times - but within the last few weeks the code appears to have stopped working.



I attach the code as a text file. In addition, here is a link to the survey with the relevant question in:



login.qualtrics.com/jfe/preview/SV_d4MYJ8FTOzV44qV?Q_SurveyVersionID=current&Q_CHL=preview



If you skip to the fifth page of the survey, then you will see the page where you have to type "ab" 100 times - but at the moment, the next button is showing from the start.



In case it helps, to get this javascript code actually count up the amount of "ab"s that are typed, I added an authenticator to the end of the survey, and some embedded datafields at the beginning.



!

!



Finally, assuming we can get the code working again, I wondered if it would be possible to set the number of "ab"s required to get the next button to show set by an embedded datafield. At the moment, I haven't been able to get JavaScript to import numbers from embedded data fields.



Thanks so much to anyone who reads or tries to help me with this!



Josh
Josh,



Try changing your function from addOnload to addOnReady. The Next button usually isn't available on load, so the var nb line is probably causing an error. If you switch to addOnReady then you won't need need the hideEl function anymore.



You can import an embedded data field as an integer number like this:

```

var a = parseInt("${e://Field/a}");

```
That tweak worked perfectly - thank you!
Hello! I just want to ask a follow up question. I use the attached code early in the survey, and then I have the exact same question again with the exact same code later in the survey, and this time, it just doesn't show the next button anymore. Do I need to reset anything to make it work a second time?





Here is the code i use:



Qualtrics.SurveyEngine.addOnReady(function()

{



choices = [];

count = 0;

payout = 0;

payoutAmount = .00;

perCount = 9999;

baseFee = 1;

totalPayout = 0;





function hideEl(element) {

element.hide();

}

var nb = $('NextButton');

hideEl.defer(nb);



var that = this;



//Key Codes: A=65, B=66

Event.observe(document, 'keydown', function (e) {



choices.unshift(e.keyCode);

choices = choices.slice(0,2);





if(choices[0] == 66 && choices[1] == 65 && count >= 1 )

{

count++;

$('count').innerHTML = count;

payout += .01;



Qualtrics.SurveyEngine.setEmbeddedData('Count',count);



nb.show();



}





if(choices[0] == 66 && choices[1] == 65 && count < 1 )

{



count++;

$('count').innerHTML = count;

payout += .01;



Qualtrics.SurveyEngine.setEmbeddedData('Count',count);



hideEl.defer(nb);

nb.hide();

}







if(count == 0)

payout = 0.00;



else {

payout = payoutAmount*Math.floor(count/perCount);

totalPayout = parseFloat(payout+baseFee).toFixed(2);

payout = parseFloat(payout).toFixed(2);

}





Qualtrics.SurveyEngine.setEmbeddedData('Total_Payout', totalPayout);

Qualtrics.SurveyEngine.setEmbeddedData('Payout', payout);

Qualtrics.SurveyEngine.setEmbeddedData('perCount', perCount);

});







});



Many thanks!



Josh
Actually, I think I answered this: have to tell it to stop observing the event from the previous page:



//Stop Observing from the previous block

Event.stopObserving(document, 'keydown');



Took me a while.



Josh

Leave a Reply