Toggle between list elements using the inner text of the elements with jQuery

My issue lies in figuring out why my filter function isn't properly working for toggling li elements.


JavaScript:

$("#searchbox1").on("keyup", function() {
   var value = $(this).val().toLowerCase();
   $("#menulist li").filter(function() {
      $(this).toggle($(this).children("span").text().toLowerCase().indexOf(value) > -1)
   });
});  

HTML:

<ul name="menulist" id="menulist" class="list-group grid-container">
    <li class="list-group-item d-flex flex-row justify-content-center align-items-center">
        <span>Yeni ürün ekle</span>
        <div class="btn-group btn-group-sm ml-auto" role="group">

            <a class="btn btn-success" href="/restaurant/additem">
                <i class="fas fa-plus"></i>
            </a>

        </div>
    </li>
    <li name="bira" class="list-group-item d-flex flex-row justify-content-center align-items-center">
        <span>bira</span>
        <div class="btn-group btn-group-sm ml-auto" role="group">
            <a href="/restaurant/edititem/-10" class="btn btn-success"><span class="fas fa-edit"></span></a>
            <a href="/restaurant/delete/-10" class="btn btn-danger"><span class="fas fa-trash-alt"></span></a>
        </div>
    </li>
    <li name="patates kızartması" class="list-group-item d-flex flex-row justify-content-center align-items-center">
        <span>patates kızartması</span>
        <div class="btn-group btn-group-sm ml-auto" role="group">
            <a href="/restaurant/edititem/-11" class="btn btn-success"><span class="fas fa-edit"></span></a>
            <a href="/restaurant/delete/-11" class="btn btn-danger"><span class="fas fa-trash-alt"></span></a>
        </div>
    </li>
</ul>

Desired effect:

I'm aiming for a simple search box functionality, but the list items aren't reacting as expected. When I experiment with code like this:

$("#menulist span").filter(...)

All span elements toggle based on the search value, however, my goal is to hide the li elements that do not match the search value. There seems to be something missing, but what could it be?

Answer №1

Your filtering code is perfectly on point! No JavaScript changes necessary.


What's the main issue here?

All your li elements are equipped with the Bootstrap 4 class d-flex. This becomes problematic during list filtration, as it will render non-matching elements with CSS property display: none;. Unfortunately, this alteration cannot be implemented due to the d-flex class being marked as an !important value, resulting in any attempt to hide li elements overridden by display: flex !important from d-flex.


How do we troubleshoot this?

The solution lies in removing the d-flex class from all li elements and instead setting them to display: flex; using custom CSS.

.list-group-item {
  display: flex;
}

Illustrative Example:

Observe the corrected version on JSFiddle.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Difficulty with Bootstrap 4 mobile navbar dropdown feature

<div class="baslik baslik1 baslik2 "> <nav class="navbar bg-light navbar-light navbar-expand-sm sticky-top "> <a href="./index.html" class="navbar-brand"><img src="img/512x512logo.png" ...

The inputs are not responding to the margin-left: auto and margin-right: auto properties as expected

I'm having trouble aligning an input in a form to the center. Even though I have included display: block in the CSS, using margin-left:auto and margin-right: auto is not yielding the desired result. To illustrate the issue more clearly, I've cre ...

Validation needed for data list option

Here are all the details <form action="order.php" method="post" name="myForm" onsubmit="return(validate());"> <input type="text" list="From" name="From" autocomplete="off" placeholder="From Place"> <datalist id="From"> <option valu ...

Formatting text over an image

The challenge I am facing involves having an image and text on it in two separate divs with a background color. The length of the text in one div is long while the text in the second div is short, but I want to ensure that the width of both divs are equal. ...

AngularJS is restricting the use of square brackets within the URL parameter, specifically the character '[.'

My goal is to connect to an external API Everything works smoothly when my parameters are set up like this $http.post('http://api.myprivatebox.com/users.json', { email : email, password : password}).then(function (results) { console.log( ...

The options in the drop-down menu appear to be stuck and remain in place

Can someone with expertise lend me a hand? I've been racking my brain over this issue, and while I feel like I'm on the right track, I just can't seem to make my sub-sub menu items drop down to the right on hover. I suspect there might be c ...

CSS an act of misbehavior

I have noticed some unusual behavior on my website page. My website design can be viewed at Also, here is the same design in CMS format Please pay attention to the section under <div class="godelpage"> <a href="http://ryugaku.babonmultimedia ...

Issue with Bootstrap rows overlapping on smaller screens

Basically, the issue I'm facing is that when I reduce the size of my screen, the columns drop below and start overlapping with the row beneath them. I've been trying to figure out if there's a simple solution that I'm overlooking. But ...

PHP Bootstrap Confirmation Dialog Tutorial

Is there a way to implement a delete confirmation dialog? When 'yes' is clicked, the message should be deleted, and when 'no' is clicked, the delete operation should be canceled. At present, this is how it looks in my view: <a href ...

Alert classes are not included in bootstrap 5 npm package

In my Angular 13 project, I am utilizing Bootstrap version 5.2.1. While most components are working as expected, the alert component seems to be missing some styling elements. Specifically, I have noticed that alerts with the classes alert-success and aler ...

``save changes to database in real time without requiring a page refresh or navigating

I have created a PHP page that includes a table displaying records from a MySQL database. One of the fields in the table, namely "housing," can have two possible values: 0 and 1. When a student is housed, the value of this field is set to 1; otherwise, it ...

Create messages similar to StackOverflow's "toast" notifications

When developing my web application, one aspect that constantly occupies my thoughts is the presentation of messages to the end-users. I've experimented with message boxes similar to those found in windows applications, but they tend to look unpleasin ...

Tailored alignment of checkboxes and labels

I've designed a custom checkbox where the actual checkbox is hidden and a label is styled to look like a checkbox. Everything works fine, however, when I add a label, it doesn't align properly. Here's the link to the fiddle: http://jsfiddle ...

Unable to define the color of icons path using CSS in Vue 3

When using the iconify library for VueJS, the icons' paths automatically have "currentColor" as their fill color. The issue arises when trying to set a path's color via CSS, as it seems to be ineffective. Even with "!important", the color won&apo ...

Creating a new component when a click event occurs in React

Currently diving into the world of React while working on a project that involves mapbox-gl. I'm facing an issue where I can successfully log the coordinates and description to the console upon hover, but I can't seem to get the popup to display ...

Is there a way to change the border property of an element when clicked, without causing the displacement of other elements?

I'm in the process of creating a webpage where users can choose the color and storage capacity of an item. Only one color/capacity can be selected at a time, and once chosen, it should be highlighted with a border. The issue I encountered is that whe ...

Utilizing jQuery to transmit an incessant stream of Ajax .post variables to the server

Is there a way to continuously send updates to the server using jQuery with minimal intervals? This is specifically for updating the cursor position of one person on others' screens, and will be used in an HTML5 whiteboard prototype. What options ar ...

Filtering HTML tables to display only one instance of each value

I have a 300x11(rowXcolumn) html table that I wanted to filter like Excel or Google Sheets's filter. Upon researching, I came across the code below on a website. While it works as desired, there is an issue where it displays the same value multiple ti ...

What is the best way to display "No results found" in Mat-select-autocomplete?

I am working with the mat-select-autocomplete for a multiselect dropdown. When searching for values that are not in the list, I want to display a message saying "No results found". Can someone please help me achieve this? Link to Code ...

Nodes in force-directed graphs are magnetically attracted to the central point

After finding a solution to the issue in this question Insert text inside Circle in D3 chart I have encountered an unexpected behavior where my nodes are not positioned correctly. I am unsure about which property is controlling the x and y coordinates of ...