I can't figure out something that is probably quite simple to do. I have a grid with cards that look like this:
<div >
<img src="data:image/jpg;base64,{{ books[book]['cover']}}" alt="...">
<div >
<span ><h5 >{{ book }}</h5></span>
<span ><h5 >{{ books[book]['author'] }}</h5>
</div>
</div>
I have an input field called search-authors and want to hide cards that do not contain whatever the value of search-authors is.
I'm trying to use this:
$("#search-authors").on("keyup", function() {
var value = $(this).val().toLowerCase();
$(".card *").filter(function() {
$(".card").css('display', 'none')
.filter(':contains(' value ')')
.css('display', 'block');
});
});
This isn't working the way I expect it to. For example, typing just c shows only cards which contain c but typing co hides cards that contain co and did show up correctly typing just c.
Any pointers would be greatly appreciated!
Thanks
CodePudding user response:
There's a couple of issues in your code.
- You need to select the
.cardelements, not their child elements, and hide/show them directly. - Use the
inputevent instead ofkeyup. The former allows people to paste content in with the mouse, the latter does not. - You don't need to nest the
filter()calls. - You're using
filter()like a loop, which is unnecessary. Callfilter()directly on the.cardcollection and update the matching elements from there. :containsis case-sensitive by default, so converting the input to lowercase isn't enough. However, you can implement your own case-insensitive version of:contains(credit @HighwayOfLife in this answer)
With all that said, try this:
$.expr[':'].icontains = (a, i, m) => $(a).text().toUpperCase().indexOf(m[3].toUpperCase()) >= 0;
$("#search-authors").on("input", e => {
$(".card").hide().filter(`:icontains(${e.target.value.trim()})`).show();
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<input type="text" id="search-authors" />
<div >
<img src="data:image/jpg;base64,{{ books[book]['cover']}}" alt="...">
<div >
<span ><h5 >ABC Book 1</h5></span>
<span ><h5 >DEF Author 1</h5></span>
</div>
</div>
<div >
<img src="data:image/jpg;base64,{{ books[book]['cover']}}" alt="...">
<div >
<span ><h5 >ABC Book 2</h5></span>
<span ><h5 >DEF Author 2</h5></span>
</div>
</div>
<div >
<img src="data:image/jpg;base64,{{ books[book]['cover']}}" alt="...">
<div >
<span ><h5 >UVW Book 1</h5></span>
<span ><h5 >XYZ Author 1</h5></span>
</div>
</div>
<div >
<img src="data:image/jpg;base64,{{ books[book]['cover']}}" alt="...">
<div >
<span ><h5 >UVW Book 2</h5></span>
<span ><h5 >XYZ Author 2</h5></span>
</div>
</div>
