I’m using below jscript code to filter unordered list. I’m trying to remove <h3> Tag for the results while searching.
Example if search for Batman My current output is
Action
Batman
Adventure
I need to remove heading Adventure coz no result under this category.
<script type="text/javascript">
(function ($) {
jQuery.expr[':'].Contains = function(a,i,m){
return (a.textContent || a.innerText || "").toUpperCase().indexOf(m[3].toUpperCase())>=0;
};
function listFilter(header, list) {
var form = $("<form>").attr({"class":"filterform","action":"#"}),
input = $("<input>").attr({"class":"filterinput","type":"text"});
$(form).append(input).appendTo(header);
$(input)
.change( function () {
var filter = $(this).val();
if(filter) {
$(list).find("a:not(:Contains(" + filter + "))").parent().slideUp();
$(list).find("a:Contains(" + filter + ")").parent().slideDown();
} else {
$(list).find("li").slideDown();
}
})
.keyup( function () {
$(this).change();
});
}
$(function () {
listFilter($("#header"), $("#list"));
});
}(jQuery));
</script>
And my html is
<div id="wrap">
<h1 id="header">DVD Collection<form class="filterform" action=""><input class="filterinput" type="text"></form></h1>
<h3>Action</h3>
<ul id="list">
<li><a href="#">Batman</a></li>
<li style="display: list-item;"><a href="#">Star Trek (2009)</a></li>
<li style="display: list-item;"><a href="#">Tremors</a></li>
</ul>
<h3>Adventure</h3>
<ul id="list">
<li style="display: list-item;"><a href="#">Ice Age</a></li>
<li style="display: list-item;"><a href="#">Avathar</a></li>
</ul>
</div>
Hers the code modified. works perfectly. i have changed the HTML too.
HTML:
Javascript:
Heres the demo
http://jsbin.com/welcome/71807/
Updated Code
Add a
lito listCSS:
JS: Another chained function to the slideUp
Demo and full code here
http://jsbin.com/welcome/71862/