Welcome to the LimeSurvey Community Forum

Ask the community, share ideas, and connect with other LimeSurvey users!

Scroll to first error after submit

  • Matadeleo
  • Matadeleo's Avatar Topic Author
  • Offline
  • Premium Member
  • Premium Member
More
3 years 8 months ago #204288 by Matadeleo
Scroll to first error after submit was created 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
Code:
$(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+?
The topic has been locked.
  • Matadeleo
  • Matadeleo's Avatar Topic Author
  • Offline
  • Premium Member
  • Premium Member
More
3 years 8 months ago #204293 by Matadeleo
Replied by Matadeleo on topic Scroll to first error after submit
Changing the code to:
Code:
$(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
The topic has been locked.
  • tpartner
  • tpartner's Avatar
  • Away
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
3 years 8 months ago #204317 by tpartner
Replied by tpartner on topic Scroll to first error after submit
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.

Code:
$(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.
The topic has been locked.

Lime-years ahead

Online-surveys for every purse and purpose