My jquery filter is not functioning properly

I need help making the active class work on each category when clicked, shifting to the next one. I've tried implementing it but no luck so far.

$(document).ready(function() {
  $('.list-inline-item').click(function() {
    const value = $(this).attr('data-filter');
    if (value == 'all') {
      $('.clients').show('1000');
    } else {

      $('.clients').not('.' + value).hide('1000');
      $('.clients').filter('.' + value).show('1000');
    }
  })
})
.list-inline-item {
  color: #000000;
  background-color: white;
  font-weight: 500;
  padding: 15px;
}

.active {
  color: #b42727;
  background-color: white;
}

.list-inline-item:hover {
  text-decoration: underline;
  text-underline-offset: 5px;
  cursor: pointer;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="container-fluid clientbox text-center py-5">
  <div class="row">
    <div class="col-12">
      <ul>
        <li class="list-inline-item active" data-filter="all">ALL</li>
        <li class="list-inline-item" data-filter="B">B</li>
        <li class="list-inline-item" data-filter="C">C</li>
        <li class="list-inline-item" data-filter="D">D</li>
        <li class="list-inline-item" data-filter="M">M</li>
        <li class="list-inline-item" data-filter="U">U</li>
        <li class="list-inline-item" data-filter="C">C</li>
      </ul>
    </div>
  </div>
  <div class="row g-3">
    <div class="clients B col-6 col-md-4 col-lg-3 col-lg-3"><img class="img-fluid " src="https://interactive-examples.mdn.mozilla.net/media/cc0-images/grapefruit-slice-332-332.jpg" alt=""></div>
    <div class="clients B col-6 col-md-4 col-lg-3 col-lg-3"><img class="img-fluid " src="https://interactive-examples.mdn.mozilla.net/media/cc0-images/grapefruit-slice-332-332.jpg" alt=""></div>
    <div class="clients B col-6 col-md-4 col-lg-3 col-lg-3"><img class="img-fluid " src="https://interactive-examples.mdn.mozilla.net/media/cc0-images/grapefruit-slice-332-332.jpg" alt=""></div>
    <div class="clients B col-6 col-md-4 col-lg-3 col-lg-3"><img class="img-fluid " src="https://interactive-examples.mdn.mozilla.net/media/cc0-images/grapefruit-slice-332-332.jpg" alt=""></div>

    <div class="clients D col-6 col-md-4 col-lg-3"><img class=" img-fluid " src="https://cdn3.vectorstock.com/i/1000x1000/27/52/orange-stock-iamge-vector-37112752.jpg" alt=""></div>
    <div class="clients D col-6 col-md-4 col-lg-3"><img class=" img-fluid " src="https://cdn3.vectorstock.com/i/1000x1000/27/52/orange-stock-iamge-vector-37112752.jpg" alt=""></div>
    <div class="clients D col-6 col-md-4 col-lg-3"><img class=" img-fluid " src="https://cdn3.vectorstock.com/i/1000x1000/27/52/orange-stock-iamge-vector-37112752.jpg" alt=""></div>
    <div class="clients D col-6 col-md-4 col-lg-3"><img class=" img-fluid " src="https://cdn3.vectorstock.com/i/1000x1000/27/52/orange-stock-iamge-vector-37112752.jpg" alt=""></div>


    <div class="clients B col-6 col-md-4 col-lg-3 col-lg-3"><img class="img-fluid " src="https://interactive-examples.mdn.mozilla.net/media/cc0-images/grapefruit-slice-332-332.jpg" alt=""></div>
    <div class="clients B col-6 col-md-4 col-lg-3 col-lg-3"><img class="img-fluid " src="https://interactive-examples.mdn.mozilla.net/media/cc0-images/grapefruit-slice-332-332.jpg" alt=""></div>
    <div class="clients B col-6 col-md-4 col-lg-3 col-lg-3"><img class="img-fluid " src="https://interactive-examples.mdn.mozilla.net/media/cc0-images/grapefruit-slice-332-332.jpg" alt=""></div>
    <div class="clients B col-6 col-md-4 col-lg-3 col-lg-3"><img class="img-fluid " src="https://interactive-examples.mdn.mozilla.net/media/cc0-images/grapefruit-slice-332-332.jpg" alt=""></div>

The code is functioning correctly, but I am having trouble making the active class specific to each category. I believe I need to modify the jQuery code, but I'm uncertain about how to do it.

Answer №1

To implement this functionality, simply update the jQuery section as shown below:

$(document).ready(function (){
    $('.list-inline-item').click(function(){
        const filterValue = $(this).attr('data-filter');
        if(filterValue == 'all'){
            $('.clients').show('1000');
        }
        else{ 
            $('.clients').not('.'+filterValue).hide('1000');
            $('.clients').filter('.'+filterValue).show('1000');
        }

        $(this).addClass('active').siblings().removeClass('active')
    })
})

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

Utilizing JSON and Javascript to dynamically generate and populate interactive tables

Here's how I've structured my JSON data to define a table: var arrTable = [{"table": "tblConfig", "def": [{"column": "Property", "type": "TEXT NOT NULL"}, {"column": "Value", "type": "TEXT NOT NULL"}], "data": [{"Property ...

Enhance your webpage with a stunning background video

I'm trying to add a background video that covers the entire screen but maintains a height of 400px, similar to the one shown here. Is there a way to achieve this without using JavaScript? Below is the HTML code I currently have: <div class="pr ...

PHP script for conducting sensitivity analysis

I am working with a PHP application that calculates a final result based on user input in multiple forms. Imagine a scenario where a user enters a value of 5 on the first page, which is passed through _POST to the next page. Then, they enter a value of 2 ...

Struggles with implementing CSS Grid's dynamic column heights and expanding rows

Linked partially to Removing empty space in CSS Grid, with specific adjustments I am aiming for. Essentially, I want my rows and columns to expand and contract based on the content present. In this CodePen example, you can observe that the content of the ...

What is the best way to make an AJAX call to an endpoint that redirects from the controller to a different view while passing

In this scenario, I have implemented a search filter for a web application. The filter data is sent to a controller method where the actual filtering process takes place. However, instead of redirecting to the desired result page, the controller redirects ...

Displaying Multiple HighCharts on a single AngularJS page

As a beginner with HighCharts, I am working on adding two Highcharts to the same page that will access the same data source but display different pieces of data for each graph. For example, the categories will remain constant while the series[] will vary. ...

Multi-line auto-suggest list with typeahead functionality

Currently, I am utilizing typeahead with Twitter Bootstrap and AngularJS. The autocomplete suggestions are displayed in a single line, as seen in the screenshot below. However, I would like to have the big cities' names displayed on separate lines. Fo ...

Step-by-step guide to implementing a user-friendly search input field using the powerful AngularJS material design framework

I am searching for an effortless method to implement a feature similar to the expandable search text field in angular-mdl. By clicking on a search button, it will expand into a text field. <!-- Expandable Textfield --> <form action="#"> < ...

Issue with interactive rows in a table ( Rails Version 4 )

I am encountering an issue with a table that is linked to a customer show page _table.html.erb <table class="table table-striped table-bordered table-hover table-condensed"> <thead> <tr> <th width="50"><%= sort_link ...

I am curious if there is a feature in intro.js that allows for the highlighting of text or images to

I am having trouble getting intro.js to work with Ionic 4 as the highlighted text is not showing up view image here This is how I implemented the code in Angular 7: intro() { let intro = introJs.introJs(); intro.setOptions({ exitOnOverlayClick: false, ...

Having trouble with Javascript in getting one-page scroll navigation to work?

Hey there, I am working on creating a one-page scroll navigation with some basic javascript to add a smooth animation effect that takes 1 second as it scrolls to the desired section. However, I seem to be experiencing an issue where it's not functioni ...

Showcasing Portfolio Work in a User-Friendly Mobile Design

Currently revamping my portfolio website and looking for ways to optimize the display of my personal projects. I have a card-like interface in place that works well on desktop but only shows one project at a time on mobile devices. Seeking solutions to imp ...

Can you show me how to magnify multiple images when hovering over them without altering their dimensions?

I am working on a gallery that features multiple images and I want the images to zoom in when hovered over without resizing the container. My project is built using Bootstrap 4. <div class="row"> <div class="col-12"><img ...

Dealing with a syntax error in JavaScript (Codecademy)

I have been working my way through the JavaScript course on Codeacademy and for the most part, I've been able to figure things out on my own. However, I've hit a roadblock with my code and can't seem to get it right. Here is the code I&apos ...

Using JavaFX to create a TreeTableView with nodes of varying sizes

I need assistance with styling TreeTableViews and their rows. I have set up a treetableview showcasing objects named Component, divided into classes A, B, and C. In my setup, B objects are nested within A, and C objects are nested within B objects. While I ...

What could be preventing the background color from changing when attempting to use style.backgroundColor?

My objective is to store the current tab background color in a variable and then use that variable to change the body color. However, for some reason it is not working as expected. Can you help me figure out why? const tabName = 'someId'; var ...

Is there a way to adjust the image dimensions when clicked and then revert it to its original size using JavaScript?

Is there a way to make an image in an HTML file change size by 50% and then toggle back to its normal size on a second click using JavaScript? I've attempted to do it similar to the onmouseover function, but it doesn't seem to be working. Any sug ...

New HTML output is displaying strangely when using jQuery AJAX

My jQuery AJAX function retrieves new HTML content from a PHP script. The PHP script is functioning correctly and sending back the accurate HTML to jQuery. However, when I utilize the html() function, the displayed HTML is incorrect. To verify the returned ...

Styling a component next to another using CSS

Struggling with a simple question here. I'm experimenting with HTML and CSS, trying to create a page with a central content box flanked by two arrow images on each side. While the concept isn't too difficult, I'm running into issues with usi ...

How can you use AJAX and jQuery to animate one container and fade in another when $_GET['var'] is set and new content is loaded?

There is a <table id="pickups"> located in the file /pages/home.php. By default, the file index.php includes home.php if no other page is specified. When you click on an element (logfile) in a specific column of the table, the container with the clas ...