Jquery/Javascript regex: filter, allow multiple tags to be typed in to select that category, jsfiddle -
http://jsfiddle.net/nicktheandroid/atknw/
the goal take value of textbox, have multiple keywords in seperated space, filter list show item(s) contain of keywords. right now, can type keywords in listed in <span class="tags">
.
i able type them in, out of order, , have still keep item keywords visible item. think might have use .split(" ")
somewhere in here... hmm...
<li>entertainment <span class="tags">tv radio dance opera</span> </li>
jquery:
$("#filter").keyup(function () { var filter = $(this).val(), count = 0; var length = $(this).val().length; if (length > 1) { $(".filtered li").each(function () { if ($(this).text().search(new regexp(filter, "i")) < 0) { $(this).addclass("hidden"); } else { $(this).removeclass("hidden"); count++; } }); } else { $('.filtered li').removeclass("hidden") count++; } $("#filter-count").text(count); });
would know how accomplish this?
exactly suspected, you'll need use string.split() split user input, , match words one-by-one against tags.
modified code as:
$("#filter").keyup(function() { var filter = $(this).val(), count = 0; var length = $(this).val().length; if (length > 1) { var filter_tags = filter.split(" "); // split user input spaces $(".filtered li").each(function() { var $this = $(this); var matches = true; // match each splitted string against whole tags string $.each(filter_tags, function(i, a_filter) { if ($this.text().indexof(a_filter) === -1) { matches = false; } }); if (matches) { $this.removeclass("hidden"); count++; } else { $this.addclass("hidden"); } }); } else { $('.filtered li').removeclass("hidden") count++; } $("#filter-count").text(count); });
you might need pass smart regexp split allow both spaces , commas separators.
working example @ http://jsfiddle.net/atknw/31/
Comments
Post a Comment