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);
});
});