Can a script rerun if a user goes back to an already visited block? | XM Community
Skip to main content
Solved

Can a script rerun if a user goes back to an already visited block?


Forum|alt.badge.img+1

I have a survey where I’ve customised the multiple choice function and text entry features to create an accordian type survey. The issue was when a user expands an accordian item, enters some text and then collapses it again it means the answer can’t be submitted, as you get an “You have added text into an unselected answer choice” error.

 

To get around this, I used a bit of jQuery to recognise when a textarea has content and then assign a class active to the Label Wrapper.

 

jQuery('.LabelWrapper textarea').keyup(function(){
  
  if(jQuery(this).val()){
    jQuery(this).parent().find('label').addClass("active");
  }else{
    jQuery(this).parent().find('label').removeClass("active");
  }
});

 

Then I can change the icon to a tick and make the answer uncollapsible using CSS.

 

 

At this point I thought I’d cracked it, but I’m using blocks in this survey and a Table of Contents (it’s what the client has specified). If a user goes back to this block having made entries to this page then the active class is removed and the script doesn’t run again, rendering my efforts useless. What I’d like to happen ideally is either the class on the LabelWrapper is retained when you return to the block, or the script runs again. Please can anybody help? Hope I’ve explained this OK, test survey link is below. The block in question is block 2.

https://stantec.ca1.qualtrics.com/jfe/form/SV_easbRxLBLZKGQXI

Best answer by ahmedA

You could just run the script everyime the page loads:

Qualtrics.SurveyEngine.addOnReady(function () {
	jQuery(".LabelWrapper textarea").each(function () {
		if (jQuery(this).val().trim()) {
			jQuery(this).parent().find("label").addClass("active");
		} else {
			jQuery(this).parent().find("label").removeClass("active");
		}
	});
});

 

View original

4 replies

Forum|alt.badge.img+22
  • Level 7 ●●●●●●●
  • 2028 replies
  • Answer
  • January 28, 2025

You could just run the script everyime the page loads:

Qualtrics.SurveyEngine.addOnReady(function () {
	jQuery(".LabelWrapper textarea").each(function () {
		if (jQuery(this).val().trim()) {
			jQuery(this).parent().find("label").addClass("active");
		} else {
			jQuery(this).parent().find("label").removeClass("active");
		}
	});
});

 


Forum|alt.badge.img+1
  • Author
  • 4 replies
  • January 28, 2025

Thanks ahmedA, that’s great and solves the problem of keeping the active class when a user goes back to the block. But now we’ve lost the functionality of the class being assigned when a user types into the text entry box. Am now trying to work on a solution combining the “each” with “keyup” to solve the issue. Haven’t got there yet.


Forum|alt.badge.img+22
  • Level 7 ●●●●●●●
  • 2028 replies
  • January 28, 2025

Just add your Keyup code inside the addOnReady function and both should work together.


Forum|alt.badge.img+1
  • Author
  • 4 replies
  • January 28, 2025

That did work and I had tried it but was still getting errors, turns out it was some CSS I was using to show and hide the textareas when collapsed/uncollapsed that was causing the problem. Have done this another way now and everything works. Thanks you so much ahmedA!


Leave a Reply