Hello I want to implement search functionality to the card list.
So my plan is to go through all the elements (using for loop) named, "card" and check if input value matches or not.
So first I add "hide" class to all the element then If input value matches with the card class's inner HTML then I will remove hide class and add "show" class.
But the problem is in the 6,7 no. line. saying removeClass /addClass is not a function.
whenever I'm using $('.name')[i] I'm getting that error. Can anyone please help?
HTML:
<input id="search" type="text" class="styled-input" placeholder="search here..."/>
<ul id="contact-list" class="scroller">
<li class="card"> <h2> John Doe 1 </h2> </li >
<li class="card"> <h2> John Doe 2 </h2> </li>
<li class="card"> <h2> Mr. Anderson </h2> </li> </ul>
My JS code is something like this:
1. let search = $('#search');
2. search.addEventListener("input", e => {
3. const value = e.target.value;
4. for (let i = 0; i < $(".name").length; i++) {
5. if ($(".name")[i].innerHTML.includes(value)) {
6. $(".name")[i].closest("li").removeClass("hide");
7. $(".name")[i].closest("li").addClass("show");
}
}