When I type something into the filter, for example the letter "B", the result shows me <div class="item"
with; Ball, Dinosaur "blue" and Robot.
I would like to know how I can make the filter ignore what is inside <span class="tag"
and only search inside the <p class="name"
, so that the final result is only <divs class="item"
with; Ball and Robot
$("#filter").on("keyup", function () {
var value =$(this).val().toLowerCase();
$("#box .item").filter(function () {
$(this).toggle($(this).text().toLowerCase().indexOf(value) > -1)
});
});
#box {
width: 300px;
}
.item {
display: flex;
align-items: center;
gap: 5px;
border: 1px solid;
}
.name {
text-transform: uppercase;
}
.tag {
background: silver;
padding:2px 4px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.7.1/jquery.min.js"></script>
<input id="filter" type="text" placeholder="Filter..." autocomplete="off">
<div id="box">
<div class="item">
<img src="" alt="img.jpg">
<p class="name">Radio</p>
<span class="tag">red</span>
</div>
<div class="item">
<img src="" alt="img.jpg">
<p class="name">Ball</p>
<span class="tag">red</span>
</div>
<div class="item">
<img src="" alt="img.jpg">
<p class="name">Dinosaur</p>
<span class="tag">blue</span>
</div>
<div class="item">
<img src="" alt="img.jpg">
<p class="name">Robot</p>
<span class="tag">blue</span>
</div>
</div>
2
Answers
When you look up the text, you can increase the specificity of the search to only search elements with the name class:
I added a .find() call to the element which will narrow the scope of the .text() call to only elements with the name class.
"keydown"
eventname. An Input element should also respond to i.e: copy/paste actions etc, not necessarily coming from a keyboard. Instead, use rather the"input"
event.filter()
if there’s nothing to filter. Use jQuery’s.each()
insteadthis
‘s.name
child using$(".name", this)
or$(this).find(".name")
Example: