It prints an empty string, supposed to print "Admin" and hide the elements that don’t contains the searchBar text, keyUp is being triggeree, it just gives me an empty string when i console.log($(this).text()), searchBar is an input field to search things in a list
const searchBar = $(".users-layout .search input")
searchBar.on("keyup", () => {
if (searchBar.val() != "") {
$(".users-list-name").each(() => {
if (!$(this).text().toLowerCase().includes(searchBar.val().toLowerCase())) {
console.log($(this).text())
$(this).hide()
} else {
console.log($(this).text())
$(this).show()
}
})
}
})
<div class="users-list">
<a href="#" class="back-icon"> <i class="fas fa-arrow-left"></i></a>
<div class="content">
<div class="details">
<span class="users-list-name">Admin</span>
<p>Test Message</p>
</div>
</div>
<div class="status-dot"><i class="fas fa-circle"></i></div>
</div>
<div class="users-list">
<a href="#" class="back-icon"> <i class="fas fa-arrow-left"></i></a>
<div class="content">
<div class="details">
<span class="users-list-name">Admin</span>
<p>Test Message</p>
</div>
</div>
<div class="status-dot"><i class="fas fa-circle"></i></div>
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
2
Answers
at the end i resolved like this
this
was refering to the window. I fixed it so it grabs the actual element:let elem = $(".users-list-name")[index]