Getting variable OUT of jquery.each() function

Chud37

I have the following search function:

function filter() {
    $("table#list tr").each(function () {
        var search = $("#search").val();
        var name = $(this).find("span.name").html();
        var email = $(this).find("span.email").html();
        var ref = $(this).find("span.reference").html();
        var match = false;
        var count = 0;
        if((name != undefined) && (email != undefined) && (ref != undefined)) {
            if(name.indexOf(search) >= 0) match = true;
            if(email.indexOf(search) >= 0) match = true;
            if(ref.indexOf(search) >= 0) match = true;
            if(match) {
                $(this).removeClass("collapse");
                count++;
            } else {
                $(this).addClass("collapse");
            }
        }
    }); 
    $("#result-count").html(count + " results found.");
}

However on the last line, count is undefined, because I created it inside the function. How can I get the value outside of the $.each function?

Edit: I also just realized I'm resetting the count inside the loop so it will always = 0! How can I count the results properly?

Rory McCrossan

Declare count outside the each block. Also note that you can tidy up the logic a little too:

var count = 0;

$("table#list tr").each(function () {
  var search = $("#search").val();
  var name = $(this).find("span.name").html();
  var email = $(this).find("span.email").html();
  var ref = $(this).find("span.reference").html();

  if (name == undefined || email == undefined || ref == undefined)
    return;

  if (name.indexOf(search) >= 0 || email.indexOf(search) >= 0 || ref.indexOf(search) >= 0) {
    $(this).removeClass("collapse");
    count++;
  } else {
    $(this).addClass("collapse");
  }
}); 

$("#result-count").html(count + " results found.");

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Getting Jquery variable to reset each time click function executes

From Dev

Getting trouble in fade out function in jquery

From Dev

jQuery Global variable value out of function

From Dev

how to get variable out of function(){ $.get() return variable; } function ? in jquery?

From Dev

Getting variable value as infinity in jquery function

From Dev

Getting variable value as infinity in jquery function

From Dev

How call variable outside function .each with Jquery?

From Dev

jQuery .each function not persisting global variable

From Dev

In Python, getting each element of a variable-length list into a function call?

From Dev

Jquery: How to break out of each() and the function it's inside in?

From Dev

Jquery filter out the double class name with each function

From Dev

JQuery - Break out of $.each

From Dev

jQuery each in each function

From Dev

Jquery Global Variable Value Changed should be accessed out of the function also

From Dev

Getting data out of object variable

From Dev

Getting a local variable out of a lambaexpression

From Dev

Jquery, using value of a variable inside a function, to set the value of another variable out side of that function

From Dev

Global variable for each function

From Dev

jQuery break out of $.on that is within $.each

From Dev

jQuery each() getting same results

From Dev

$.each() function jQuery on an object

From Dev

jQuery each function $(this) confusion

From Dev

jQuery .each css is not a function

From Dev

jquery each function loop

From Dev

Each function jquery

From Dev

jQuery "Each" function values

From Dev

Each function jquery

From Dev

jquery each as 'named function'

From Dev

Simple jQuery Each Function

Related Related

HotTag

Archive