There is a searchable bar for cards heading in below snippet. It is highlighting the searched value in headings of cards
Can it be possible to count the number of cards having search value . Like I search for "pro" than it show "Result for pro is in 4 cards " . Whatever I search in bar than it show the number of cards having the searched value .
Example : Let say I typed "p" or "pro" in search bar than check in how many cards "p" or "pro" is used and count the number near search bar . Here "pro" is used in 4 cards so 4 will be displayed and "p" is used in 5 cards so 5 will be displayed
Also there is problem in program that when search for "yatch", it is showing "No result found for "yatch" " because I used the indexOf
to check if it is available or not but still showing not found even if it is there and highlighting it .
Try to type "yatch" in search . It will highlight "yatch" but also show not found near search bar.
document.getElementById("search").addEventListener("input", refree);
function refree() {
var reader = document.getElementsByClassName("deviceNameCardHead")
for (let i = 0; i < reader.length; i++) {
var readerText = reader[i].textContent
var reed = document.getElementById("search").value;
var reed1 = reed.toLowerCase();
if (reed != '') {
reader[i].innerHTML = readerText.replace(new RegExp(reed, 'gi'), (match) => `<span class="highlight">${match}</span>`);
if (readerText.toLowerCase().indexOf(reed1) > -1) {
reader[i].parentElement.style.display = "block";
document.getElementById("demo").innerHTML = "Showing " + "10(The number of cards showing the searched word) results" + " for <b>\"" + reed + "\"</b>";
} else {
reader[i].parentElement.style.display = "none";
document.getElementById("demo").innerHTML = "No result found for <b>\"" + reed + "\"</b>";
}
} else {
reader[i].innerHTML = readerText;
reader[i].parentElement.style.display = "block";
document.getElementById("demo").innerHTML = "Showing all 5 results";
}
}
}
.highlight {
background: yellow;
}
.deviceNameCardFlex {
display: flex;
flex-flow: row;
flex-wrap: wrap;
justify-content: flex-start;
}
.deviceNameCard {
display: flex;
flex-flow: column;
border: 2px solid red;
width: 40%;
height:200px;
box-shadow:3px 3px 10px;
border-radius:5px;
padding: 10px;
margin: 10px;
text-align: center;
}
<div id="devicesBtnData">
<div class="searchDevice">
<span class="searchDeviceBtn">Search Device</span>
<input id="search" type="search" placeholder="Try it">
<span id="demo"></span>
<span id="demo1"></span>
<br>
</div>
<div class="deviceNameCardFlex">
<div class="deviceNameCard">
<h3 class="deviceNameCardHead">Lenova Yoga laptop Pro</h3>
<div>Card Content</div>
</div>
<div class="deviceNameCard">
<h3 class="deviceNameCardHead">Lenova Yoga laptop Pro</h3>
<div>Card Content</div>
</div>
<div class="deviceNameCard">
<h3 class="deviceNameCardHead">No use of P in next</h3>
<div>Card Content</div>
</div>
<div class="deviceNameCard">
<h3 class="deviceNameCardHead">yatch</h3>
<div>Card Content</div>
</div>
<div class="deviceNameCard">
<h3 class="deviceNameCardHead">Lenova Yoga laptop Pro</h3>
<div>Card Content</div>
</div>
</div>
</div>