Reputation: 85
How can I search only on the b tag but not on the whole div. Now i can search on the whole div, but I only want to be able to search on the title in the b tag. And if there is a name that is in the list, it has to be removed HTML :
<div class="row">
<div class="col-md-3">
<div class="panel panel-default" id="list">
<div class="panel-header">
<div class="input-group">
<input type="text" class="form-control" placeholder="Search for users" id="searchName"/>
<div class="input-group-btn">
<div class="btn-group">
<button type="button" class="btn btn-primary">
<span class="glyphicon glyphicon-search"></span></button>
</div>
</div>
</div>
</div>
<div class="panel-body" id="user1">
<div class="row">
<div class="col-md-4">
<img src="" class="img-thumbnail">
</div>
<div class="col-md-8">
<b>Pieter</b><br>
(Laatste bericht)<br>
<i>12:01</i>
</div>
</div>
</div>
<div class="panel-body" id="user2">
<div class="row">
<div class="col-md-4">
<img src="" class="img-thumbnail">
</div>
<div class="col-md-8">
<b>Karel</b><br>
(Laatste bericht)<br>
<i>11:15</i>
</div>
</div>
</div>
<div class="panel-body" id="user3">
<div class="row">
<div class="col-md-4">
<img src="" class="img-thumbnail">
</div>
<div class="col-md-8">
<b>Pilot</b><br>
(Laatste bericht)<br>
<i>9:36</i>
</div>
</div>
</div>
</div>
</div>
Javascript :
function contains(text_one, text_two){
if (text_one.indexOf(text_two) != -1){
return true;
}
}
$("#searchName").keyup(function () {
var searchName = $("#searchName").val().toLowerCase();
$(".panel-body").each(function () {
if(!contains($(this).text().toLowerCase(), searchName)) {
$(this).hide();
}
else {
$(this).show();
}
});
});
Upvotes: 1
Views: 1098
Reputation: 10346
Use find()
in the condition to focus on the element's child B tag:
Before:
if(!contains($(this).text().toLowerCase(), searchName)) {
After:
if(!contains($(this).find('b').text().toLowerCase(), searchName)) {
Upvotes: 2