Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8974789
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T18:44:48+00:00 2026-06-15T18:44:48+00:00

In my form, I’ve added a class named required to all elements that cannot

  • 0

In my form, I’ve added a class named required to all elements that cannot be empty. Then I’m looping through each element using $.each() and adding a class named border which will apply a 2px red border to all those elements.

var req = $('.required');
$.each(req, function(){
    var curr_val = $(this).val();
    if(! curr_val)
    {
      $(this).addClass('border');
    }
});

Now there is vertical scrolling on the page and so when submit is clicked I want the page to scroll up to the first empty element so that the user will know something is wrong and that is the reason the form did not submit

WHAT I TRIED

I added this block in my earlier version of code

$.each(req, function(){
    var curr_val = $(this).val();
    if(! curr_val)
    {
      $(this).addClass('border');
      $('html, body').animate({            // added for scrolling purposes
         scrollTop: $(this).offset().top
      }, 200);
    }
});

This works, but the problem is that it will keep scrolling at every 0.2 sec interval till it reaches the last empty element. This looks very funny and undesirable to the user.

So I was wondering if there is way to make it to scroll to the first empty element is occurs and stop there even if there are more empty elements below it. When that is filled and submit is clicked again, scroll to the first empty element again and stop it right there.

JS Fiddle

Here is a JS Fiddle to get you started and to also give an idea about the funny behavior that I’m referring to. Notice how it keeps scrolling till it reaches the last element.

JS DEMO

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T18:44:49+00:00Added an answer on June 15, 2026 at 6:44 pm

    You run the animation for each element, the solution is as simple as adding a condition to do only the first time:

    var firstTime = true;
    $.each(req, function(){
        var curr_val = $(this).val();
        if(! curr_val)
        {
          $(this).addClass('border');
          if(firstTime === true)
          {
              $('html, body').animate({            // added for scrolling purposes
                 scrollTop: $(this).offset().top
              }, 200);
              firstTime = false;
          }
        }
    });
    

    Here is your fiddle updated.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Form is made from a function that prints out all rows that are awaiting
/* Form Elements & Other Definitions Here ... */ $this->setAction(auth) ->setMethod(post) ->setAttrib(class,ajax_form); $email =
Form docs: Comparable c=mock(Comparable.class); when(c.compareTo(anyInt())).thenReturn(-1); I would like: File tempDir=new File(test); Comparable c=mock(Comparable.class); when(c.compareTo(anyInt())).thenReturn(tempDir.mkdir());
Form setup: QSplitter (highlighted on the image) that contains QTreeWidget (left) and QTableWidget (right):
Form Build your own MVVM I have the following code that lets us have
The form here works fine in the browser, but for some reason I cannot
Form/Type class DemoType extends AbstractType { public function buildForm(FormBuilderInterface $builder, array $options) { $builder->add('name',
//--form timesheettype---it is not entity class class TimeSheetType extends AbstractType { public function buildForm(FormBuilder
Windows Form Application – Manipulating input-elements in WinForm WebBrowser Although I am familiar with
form that causes the problem: <%= form_for Like.find(post.user.likes), :html => { :method => :delete

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.