1

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?

1
  • put var count = 0; outside .each() Commented Apr 26, 2017 at 7:30

3 Answers 3

1

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.");
Sign up to request clarification or add additional context in comments.

Comments

1

As you said, just have it off the each loop:

function filter() {
   var count =0; // put it here
$("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;
    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.");
} 

Comments

0
function filter() {
    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();
        var match = false;
        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.");
}

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.