Scroll to first error after submit

More
6 months 1 week ago #204288 by Matadeleo
Hi guys, I'm trying to set up my survey so after you press "Next", if there are errors on the page then I would like to scroll to the first error. I saw a previous solution (from 7 years ago) where I had to put the below code into my theme.js
$(document).ready(function() {
 
	if($('.errormandatory').length > 0) {
		$('html, body').animate({
			scrollTop: $('.errormandatory:first').offset().top
		}, 2000);
	}
});

I assume this is no longer working due to the age of the solution, is there a way to do this in LS 3.5+?

Please Log in to join the conversation.

LimeSurvey Partners
More
6 months 1 week ago #204293 by Matadeleo
Changing the code to:
$(document).ready(function() {
 
	if($('.ls-question-mandatory').length > 0) {
		$('html, body').animate({
			scrollTop: $('.ls-question-mandatory:first').offset().top
		}, 2000);
	}
});

Doesn't work whilst you are in the survey, but if you close it and then come back to it, it correctly scrolls you to the first mandatory error

Please Log in to join the conversation.

More
6 months 1 week ago #204317 by tpartner
It does work for me in version 3.22.210.

Your problem in that older version may be that you have AJAX enabled - always disable AJAX in the survey theme options. (and update to the latest 3.x version)

You may also want to add a little more offset to account for the header height.

$(document).ready(function() {
 
	if($('.ls-question-mandatory').length > 0) {
		$('html, body').animate({
			scrollTop: $('.ls-question-mandatory:first').offset().top - 160
		}, 2000);
	}
});

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
Official LimeSurvey Partner - partnersurveys.com

Please Log in to join the conversation.

Start now!

Just create your account and start using Limesurvey today.

Register now