1

This is my script (simplified):

var $error = false;
//Check for errors
$('.required').each(function()
{
    var $error = true;
});
alert($error);

How to make the above code return true in the alert function? The each function is executed 3 times at my page to check for empty fields. But in the above the alert contains false instead of true. Is there a way to parse the variable $error outside the each function to check if there are empty fields after the each loop?

3
  • If you're been alerted false, then you have no elements that match the .required selector. Try fixing this first :) Commented Jun 6, 2013 at 14:52
  • 1
    Related SO question and Wikipedia article. Commented Jun 6, 2013 at 14:54
  • Thanks for the article link! Commented Jun 6, 2013 at 15:01

5 Answers 5

3

Drop the var inside the .each() callback function, otherwise you are defining a new variable in a different scope.

var $error = false;
//Check for errors
$('.required').each(function()  //<-- start of new scope block
{
    var $error = true;  //<--defines new variable -- different from one above
});
alert($error);

Dropping var, it will now use the variable defined in the block above it

var $error = false;
//Check for errors
$('.required').each(function()  
{
    $error = true;  
});
alert($error);
Sign up to request clarification or add additional context in comments.

Comments

3

You are shadowing the outer $error variable inside the .each() callback. Get rid of the var so you reference the outer one instead:

var $error = false;
$('.required').each(function() {
    $error = true; // Now this refers to the `$error` declared above
});

However, if you're simply using the .each call to check if any elements have the .required class, you could just check .length:

alert($('.required').length > 0); // true or false depending if elements matched

2 Comments

For more information: Variable shadowing in Javascript
Thanks a lot all! Learned something small but very usefull again today! Plus your second way is a lot easier. I will use it. I was now using: $(this).val()=='' instead.
3

just remove the inner var keyword

var $error = false;
//Check for errors
$('.required').each(function()
{
     $error = true;  // No Var Here
});
alert($error);

or use window as your namespace like the following

window.error = false;
//Check for errors
$('.required').each(function()
{
     window.error = true;  // No Var Here
});
alert(window.error);

Note its better to break the loop if you got an error with return false -- just a performance advice !

Comments

2

You $erro declared locally and globally

var $error = false;
$('.required').each(function()
{
    $error = true;
});

Comments

0

If you want to check if the texts are empty you can use:

var $error = false;
$('input.anyclass').each(function()
{
    if($(this).val() == "")
    {
        $error = true;
    }
});
alert($error);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.