I have a photo gallery grid and I am looking to implement a search functionality for an img tag's 'data-title' value, where only images with matching data will be displayed. For example, if I type "hay" into the search box, only the image below should appear, while all other images without "hay" in their data-title will be hidden.
<a href="photo_gallery\photos\01.jpg" data-lightbox="gallery" data-title
="Hay Bales - I love hay bales. Took this snap on a drive through the
countryside past some straw fields.">
<img src="photo_gallery\photos\thumbnails\01.jpg" alt="fields">
</a>
I tried customizing some code found here to hide divs but ran into difficulty when trying to search the data-title attribute. Every time I type something in the search box, all images get hidden regardless of their data-title attribute.
$('#search').keyup(function() {
var value = $(this).val();
var exp = new RegExp(value);
$('a').each(function() {
var isMatch = exp.test($('a[data-title='+value+']', this).text());
$(this).toggle(isMatch);
});
});
Can someone point out what I might be doing wrong here? This is how the search bar is currently structured.
<div class="searchBar">
<input type="text" name="search" id="search" placeholder="Search">
</div>