I have been looking at this jQuery quick filter code here: https://github.com/syropian/jQuery-Quick-Filter
I'd like to be able to use it to quick filter a list of images, instead of list items as used in the demo.
The demo uses this:
<script type="text/javascript">
$(document).ready(function(){
$('#txtSearch').quickfilter('#list li');
});
</script>
...
<input type="text" id="txtSearch" placeholder="Filter" />
<ul id="list">
<li>Apples</li>
<li>Oranges</li>
<li>Pineapples</li>
<li>Bananas</li>
<li>Dragonfruit</li>
<li>Peaches</li>
<li>Raspberries</li>
<li>Strawberries</li>
<li>Blueberries</li>
<li>Cantaloupe</li>
</ul>
I'd like to do this:
<script type="text/javascript">
$(document).ready(function(){
$('#txtSearch').quickfilter('#list img');
});
</script>
...
<input type="text" id="txtSearch" placeholder="Filter" />
<div id="list">
<img src="a.png" width="5" height="5" title="Apples" />
<img src="a.png" width="5" height="5" title="Oranges" />
<img src="a.png" width="5" height="5" title="Pineapples" />
<img src="a.png" width="5" height="5" title="Bananas" />
<img src="a.png" width="5" height="5" title="Dragonfruit" />
<img src="a.png" width="5" height="5" title="Peaches" />
<img src="a.png" width="5" height="5" title="Raspberries" />
<img src="a.png" width="5" height="5" title="Strawberries" />
<img src="a.png" width="5" height="5" title="Blueberries" />
<img src="a.png" width="5" height="5" title="Cantaloupe" />
</div>
In my case, I'd like to be able to filter based on the image's title attribute.
The jQuery Quick Filter code is this:
(function($){
$.extend($.expr[':'], {missing: function (elem, index, match) {
return (elem.textContent || elem.innerText || "").toLowerCase().indexOf(match[3]) == -1;
}});
$.extend($.expr[':'], {exists: function(elem, i, match, array){
return (elem.textContent || elem.innerText || '').toLowerCase().indexOf((match[3] || "").toLowerCase()) >= 0;
}});
$.extend($.fn,{
quickfilter: function(el){
return this.each(function(){
var _this = $(this);
var query = _this.val().toLowerCase();
_this.keyup(function () {
query = $(this).val().toLowerCase();
if(query.replace(/\s/g,"") != ""){
$(el+':exists("' + query.toString() + '")').show();
$(el+':missing("' + query.toString() + '")').hide();
}
else {
$(el).show();
}
});
});
}
});
})(jQuery);
I wonder if anyone might be able to provide a pointer about how I can modify it to search on the title attributes please?
I realise the function uses "innerText", and searches on the inner text between the <li>
list items.
In this case, it's slightly different as it needs to search the attribute.
The quickfilter.js checks for elements textcontent and innertext only. Adding a condition to check for image title will do the job. Add elem.title as per below code in quickfilter.js
I think changing the selector to your case should work:
If not, please provide a Fiddle with your try in the comment section below.
Replace this:
with this:
SNIPPET
this is not working ?
without quickfilter