Reputation: 3
I am having trouble getting this code to run. I want to do a live search and hide any photos with captions that do not match the search text. I would appreciate any tips!
Thanks!
Upvotes: 0
Views: 618
Reputation: 7250
This would be fairly efficient:
let $search = $('#search');
let $imgs = $('img')
$search.keyup(event => {
let value = $search.val().toLowerCase();
$imgs.show();
if (value !== '') {
$imgs.not('[title*="' + value + '"]').hide();
}
});
Try it: https://jsfiddle.net/ct8jgvyd/1/
edit: code without arrow functions etc
var $search = $('#search');
var $imgs = $('img')
$search.keyup(function(event) {
var value = $search.val().toLowerCase();
$imgs.show();
if (value !== '') {
$imgs.not('[title*="' + value + '"]').hide();
}
});
Upvotes: 1