Using JavaScript to display an alert on screen when an incorrect response is given (onUnload) | XM Community
Skip to main content
Hi all, currently attempting to have an alert pop up on screen upon selection of a particular answer for a singlecode question, before the question passes and moves on (still recording the response they selected that caused the pop up to appear - this pop up will basically just be a warning message). Ideally would be based on recode value so can just copy across the JS for future questions and swap out recode.



So currently i've got the below, but this simply produces the error alert regardless of what option is selected. As a complete JS rookie, I have been unable to get past this!



Qualtrics.SurveyEngine.addOnUnload(function()

{

var q1 = parseInt(document.getElementById('QR~QID1/ChoiceDescription/3')),

msg = 'INSERT ERROR TEXT HERE';

if (q1,1)

{

alert(msg);

document.getElementById('QR~Q1D1/ChoiceDescription/3').focus();

return false;

} else {

return true;

}

});



Anyone able to help would be greatly appreciated!



Cheers.
** side note - the above "QR~QID1/ChoiceDescription/3" is aiming to use the 3rd response choice rather than the recode for this choice - would prefer to swap this out for recode value but not absolutely necessary!
Hello @WillBush94 ,



Paste the below code in the js(OnReady) of the Multi-choice single select question type.



var that = this;

var msg = 'INSERT ERROR TEXT HERE';

jQuery("#NextButton").on('click',function(){

var correctrecode = 8;

var choiceid = jQuery("#"+that.questionId+" input[type='radio']:checked").attr("choiceid");

var selectedrecode = that.getChoiceRecodeValue ( choiceid );

if(correctrecode==selectedrecode){

alert(msg);

}



});



You need to update the var correctrecode value with required recode value of option in the above code
Hi Shashi - brilliant thank you so much!

Leave a Reply