As a beginner, I am currently on my second project involving Bootstrap and JS. For this project, I have created a search form and multiple cards with two paragraphs each.
<div class="container">
<div class="row justify-content-center my-5">
<input class="form-control col-xs-12 col-lg-10 col-sm-8" id="myInput" type="text" onkeyup="myFunction()" placeholder="Search">
<button class="btn btn-outline ml-2 my-xs-2 col-xs-8 " style="font-family: Arial, Helvetica, sans-serif; font-weight: 700;">SEARCH</button>
</div>
</div>
<div class="container">
<ul class="row" id="myList" style="list-style-type: none;">
<li class="card mb-4 box-shadow"><p>Adele</p><p>2001</p></li>
<li class="card mb-4 box-shadow"><p>Agnes</p><p>1980</p></li>
<li class="card mb-4 box-shadow"><p>Billy</p><p>2010</p></li>
<li class="card mb-4 box-shadow"><p>Bob</p><p>1530</p></li>
<li class="card mb-4 box-shadow"><p>Calvin</p></li>
<li class="card mb-4 box-shadow"><p>Christina</p></li>
<li class="card mb-4 box-shadow"><p>Cindy</p></li>
</ul>
</div>
Below is my script:
function myFunction() {
var input, filter, ul, li, p, i, txtValue;
input = document.getElementById('myInput');
filter = input.value.toUpperCase();
ul = document.getElementById("myList");
li = ul.getElementsByTagName('li');
for (i = 0; i < li.length; i++) {
p = li[i].getElementsByTagName("p")[0];
txtValue = p.textContent || p.innerText;
if (txtValue.toUpperCase().indexOf(filter) > -1) {
li[i].style.display = "";
} else {
li[i].style.display = "none";
}
}
}
While searching by name works fine, entering the year does not yield results. The issue seems to be related to how the script handles the second paragraph. Any suggestions on how to modify the script in order to address this problem?