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

  • Home
  • SEARCH
  • 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 4255360
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T05:12:32+00:00 2026-05-21T05:12:32+00:00

I have a simple jquery loop that goes through my form and sees if

  • 0

I have a simple jquery loop that goes through my form and

  1. sees if there is empty fields.
  2. If any are empty, mark them with an ’empty’ class and
  3. then create a ‘error’ variable

Basically:

// check all the inputs have a value...
$('input').each(function() {

    if($(this).val() == '') {

        $(this).addClass('empty');
        var error = 1;

    }   

});

This works a charm. However, as my code continues, I can’t seem to access that ‘error’ variable… as though it is locked inside the each loop. With the following code being right after the .each() loop, I never get my_error_function() to fire, even though I know that criteria 1 and 2 are working.

if(error == 1) {

    my_error_function();

} else {

    my_non_error_function();

}

How do I access this variable so I can use its result elsewhere in the code?

  • 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-05-21T05:12:33+00:00Added an answer on May 21, 2026 at 5:12 am

    Define your error variable outside of your function/loop

    var error = 0;
    $('input').each(function() {
    
        if($(this).val() == '') {
    
            $(this).addClass('empty');
            error = 1;
    
        }   
    
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a simple jQuery code that runs on a web page that has
I have a simple JavaScript file that has three jQuery $document.ready functions. All three
I have simple AJAX function that uses jQuery to return an array of 300
This is a very simple form that I have found on the web (as
I have a very simple for-loop in jQuery: var iEra; for(iEra = 1; iEra
I have a simple jquery animation on a div, which is inside a div,
I have a simple jquery/ajax request to the server which returns the structure and
I have a very simple jQuery script.. all I want it to do is
I have created a simple JQuery script with hovering effect on some links. The
I have written a simple jQuery.ajax function which loads a user control from the

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.