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

Issue encountered when toggling flip switch in Jquery Mobile

I am trying to create a calculator for my app using Jquery Mobile, but I am facing an issue with the flip toggle switch. Here is my script: <script type="text/javascript"> function dosis() { $peso = $('#peso').get(0).value $dosis = ...

Clear Vuex state upon page refresh

In my mutation, I am updating the state as follows: try { const response = await axios.put('http://localhost:3000/api/mobile/v3/expense/vouchers/form_refresh', sendForm, { headers: { Accept: 'application/json', 'C ...

Can one image impact other images through a wrapping function?

I am facing an issue with positioning 3 images independently from the window size using the position: relative declaration. When I try to define positions for the first 2 images and then insert the third one, it disrupts the position of the first two. Is ...

"Encountered a hiccup while trying to retrieve Selector with

I have a list of 335 different gene access numbers, each corresponding to a unique URL. These URLs are used to make requests from the NCBI website GenBank. The specific data I am looking for can be found in an XPath called: //*[@id="viewercontent1"]/pre ...

Utilizing AngularJS ng-messages feature across various languages

Utilizing ng-messages to show error messages for form validation in my application. I have a multi-language app, how can I implement ng-messages to support multiple languages? HTML Form <div class="messages" ng-messages="myForm.email.$error"> & ...

Is there a way for me to receive the response from the this.$store.dispatch method in vue.js 2?

Here is the structure of my component : <script> export default{ props:['search','category','shop'], ... methods: { getVueItems: function(page) { this.$store.disp ...

Guide for using a CSS variable in a dynamic CSS class within a dynamic component

I'm working with library components and running into an issue with CSS when importing the same component multiple times within a parent component with different styles. import "../myCss.css" const CircleComponent = ({size , color}) => { ...

How to showcase images and text from an array of objects using React

I'm currently working on a React lightbox that is supposed to display a loop of objects containing images and text. However, I'm facing an issue where the images are not showing up with the corresponding text label as intended. It seems like I a ...

VueJS advisory: Refrain from directly altering a prop

When attempting to modify a prop value using the @click directive, I encountered a warning message: [Vue warn]: Avoid mutating a prop directly since the value will be overwritten whenever the parent component re-renders. Instead, use a data or computed pr ...

Is it possible to conditionally call the Apollo Client in my Vue template script?

I have a scenario where I pass a query to the apollo client in my template file using a script tag. However, I want to find a more efficient way to handle this without having to specify the query every time. My idea is to pass a boolean value through a pro ...

Unable to retrieve a response, the operation `users.findOne()` has exceeded the buffering time limit of 10000ms

I encountered an issue when attempting to make a POST login request. The frontend is deployed on Netlify here, and the backend is deployed on Heroku here. Here are my backend logs . I am receiving `users.findOne()` buffering timed out after 10000ms in ...

Is it possible to maintain a fixed footer while utilizing async/ajax functions?

Looking for a reliable solution to have a fixed footer that adjusts based on the page content? I've tested multiple samples, but they all fall short when it comes to incorporating AJAX elements. Is there a fixed footer out there that truly works seaml ...

Is it possible to implement the Jquery blockui(plugin) code without relying on Ajax?

Hey there, I'm new to Jquery and could really use some help... I've got a page with multiple links, but when a user clicks on one link and it's still processing in the background before loading the page, they keep clicking on other links wi ...

How to break down JSON into individual elements using JavaScript

{ "name": "Sophia", "age": "Unknown", "heroes": ["Batman", "Superman", "Wonder Woman"], "sidekicks": [ { "name": "Robin" }, { "name": "Flash Gordon" }, { "name": "Bucky Barnes" } ...

Should you include the dollar sign in a Vue HTML variable or not?

I’m a bit confused about whether or not I should include $ when using a Vue HTML variable: new Vue({ data: { a: "myData" } }); Do I need to use: <h1>My value is {{ a }}</h1> or <h1>My value is {{ $a }}</h1> What ...

Having trouble iterating through an array of objects in Vue?

Having trouble looping through an array of objects in Vue3 <template> <div class="shadow-xl w-96 h-96 md:h-[600px] md:w-[600px] lg:my-12 lg:w-[700px] lg:h-[700px] rounded-md" > <button @click="getData">Get ...

Angular's input event fails to trigger for dynamically generated fields when pasted into them

When working with a form that has dynamically generated input fields, I encountered an issue. I needed to display a preview of the input contents on a div while pasting content into the fields. This project is built using Angular 11. Below is my .ts file: ...

Accessing a variable from another HTML using JavaScript

I'm currently attempting to access a variable from another HTML file using JS. Specifically, I have a file (file1.htm) that opens a dialog box, and I want to send the information of the selected file to another file (file2.htm) in order to modify a v ...

Ascending and descending functions compared to Ext.getCmp()

I'm feeling a bit lost trying to figure out which method to use when using grep object - should I go with up(), down(), or Ext.getCmp(ID)? Personally, I find it simpler to assign an ID to the object and then retrieve it using Ext.getCmp('ID&apos ...

Is there a way to locate classes containing underscores within them using Selenium?

Having trouble scraping a webpage with a class name containing an underscore, specifically this element: <span class="s-item__time-left">30m</span> == $0 I attempted to locate it by class name: time = driver.find_elements_class_name("s-item_ ...