Show Div Depending On A Search
I have a small problem with a script that I want to find and show different divs depending on a search. The original script is something I found and used for a contact list, and th
Solution 1:
The issue is because jQuery objects do not have an id
property. You need to use prop('id')
or just this.id
.
Also note that you can improve your logic by making the id
attributes you match with lower case, then convert the input to lower case, then you can just use a normal selector, like this:
$('#search').click(function() {
var txt = $('#search-criteria').val();
if (txt)
$('.fruit').hide().filter('#' + txt.toLowerCase()).show();
});
.fruit {
display: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<input type="text" id="search-criteria" />
<input type="button" id="search" value="search" />
<div class="fruit" id="apple">
<h3>Some text about apples</h3>
</div>
<div class="fruit" id="orange">
<h3>Some text about oranges</h3>
</div>
Post a Comment for "Show Div Depending On A Search"