tags:

views:

76

answers:

3

Revised Code

jQuery(function($) {
$("#filter").keyup(function () {
    var filter = $(this).val(), count = 0;
    $(".filtered h2").each(function () {
    if ($(this).text().search(new RegExp(filter, "i")) != -1) {
            $(this).parent().addClass("hidden");
    } else {
            $(this).parent().removeClass("hidden");
            count++;
    }
    $("#filter-count").text(count);
    });
});

});

I have a number of divs that are listed and I want to be able to add an input field that will allow a user to start typing and the divs are filtered according to each divs h2 tag. I'm trying the code below, but it's not filtering. It's not throwing up any errors either, so I'm not sure what to do at this point...

Here's the html markup:

    <input id="filter" name="filter" size="40"/>

    <div class="filtered">

        <div class="archive-row clearfix">
             <label>Creator: </label><h2 class="hidden">Alchemy</h2>
                <label>Title: </label> <p>Cras velit eros, eleifend ut viverra </p>
                <label>Summary: </label><p>Etiam lobortis, risus a dignissim tempor, neque sapien lobortis mi, quis semper sapien nisi dictum nisi. Integer neque neque</p>
        </div>

     <div class="archive-row clearfix">
         <label>Creator: </label><h2 class="hidden">Mathematics</h2>
            <label>Title: </label> <p>Cras velit eros, eleifend ut viverra </p>
            <label>Summary: </label><p>Etiam lobortis, risus a dignissim tempor, neque sapien lobortis mi, quis semper sapien nisi dictum nisi. Integer neque neque</p>
        </div>

     <div class="archive-row clearfix">
         <label>Creator: </label><h2 class="hidden">English</h2>
            <label>Title: </label> <p>Cras velit eros, eleifend ut viverra </p>
            <label>Summary: </label><p>Etiam lobortis, risus a dignissim tempor, neque sapien lobortis mi, quis semper sapien nisi dictum nisi. Integer neque neque</p>
        </div>

    </div>

Here's the jquery...

$(document).ready(function() {

$("#filter").keyup(function () {
    var filter = $(this).val(), count = 0;
    $(".filtered h2").each(function () {
 if ($(this).text().search(new RegExp(filter, "i")) < 0) {
            $(this).addClass("hidden");
 } else {
            $(this).removeClass("hidden");
            count++;
 }
    });
    $("#filter-count").text(count);
});

});
A: 
    <input id="filter" name="filter" size="40"/>

    <div class="filtered">

        <div class="archive-row clearfix">
             <label>Creator: </label><h2 >Alchemy</h2>
                <label>Title: </label> <p>Cras velit eros, eleifend ut viverra </p>

                <label>Summary: </label><p>Etiam lobortis, risus a dignissim tempor, neque sapien lobortis mi, quis semper sapien nisi dictum nisi. Integer neque neque</p>
        </div>

     <div class="archive-row clearfix">
         <label>Creator: </label><h2 >Mathematics</h2>
            <label>Title: </label> <p>Cras velit eros, eleifend ut viverra </p>

            <label>Summary: </label><p>Etiam lobortis, risus a dignissim tempor, neque sapien lobortis mi, quis semper sapien nisi dictum nisi. Integer neque neque</p>
        </div>

     <div class="archive-row clearfix">
         <label>Creator: </label><h2>English</h2>
            <label>Title: </label> <p>Cras velit eros, eleifend ut viverra </p>

            <label>Summary: </label><p>Etiam lobortis, risus a dignissim tempor, neque sapien lobortis mi, quis semper sapien nisi dictum nisi. Integer neque neque</p>
        </div>

    </div>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js"&gt;&lt;/script&gt;
    <script>
$(document).ready(function() {

    $("#filter").keyup(function () {
    var filter = $(this).val(), 
        count = 0;

    $(".filtered h2").each(function () {
        var parent = $(this).parent(), length = $(this).text().length>0;

        if ( length && $(this).text().search(new RegExp(filter, "i")) < 0) {
     parent.hide();
         } else {
     parent.show();
     count++;
         }
    });
    $("#filter-count").text(count);
    });

});

</script>
<div id="filter-count"></div>
meder
No... :-( Nothing happens. Still no error messages either. I tried it both the original way and your suggested way, and still nothing...
phpN00b
What exactly are you hiding? The h2s or the parent elements? *confused*
meder
I've got firebug and I don't get any errors at all... Since I originally, posted, I've made some progress, but in the wrong direction... First off, I realized that I need to hide the whole div that the h2 belongs to, so I used parent(), but when I delete the characters that I entered in the input field, everything disappears. Second, when I type in a character, it removes the h2 div that begins with that letter, rather then hiding the others... I'm putting the revised code in my original post... Guidance is much appreciated.
phpN00b
I updated my answer and it should be a little better. You should absolutely point out exactly what you want when you filter, because we don't know what *you* want if you just say "filter"
meder
Ok, nvm... Once I changed it back to 0, it worked :) Thanks!
phpN00b
I updated my answer again based on what I wanted if I were filtering.
meder
A: 

It appears you're using the .each() function on the actual H2 tags and adding/removing the hidden class from those as well. My guess is, you want to be adding/removing the hidden class from the div itself and not the H2?

$(document).ready(function() {
    $("#filter").keyup(function () {
     var filter = $(this).val(), count = 0;
     $(".filtered h2").each(function () {
      if ($(this).text().search(new RegExp(filter, "i")) < 0) {
       $(this).parent().addClass("hidden");
      } else {
       $(this).parent().removeClass("hidden");
       count++;
      }
     });
     $("#filter-count").text(count);
    });
});
WesleyJohnson
A: 

I may be wrong but I think you want to hide the parent div so a call to parent should solve this issue.

$(document).ready(function() {

    $("#filter").keyup(function () {
        var filter = $(this).val(), 
            count = 0;
        $(".filtered h2").each(function () {
            if ($(this).text().search(new RegExp(filter, "i")) < 0) {
                $(this).parent().addClass("hidden");
            } else {
                $(this).parent().removeClass("hidden");
                count++;
            }
        });
        $("#filter-count").text(count);
    });
});
ChaosPandion