Revolutionizing Gallery Filtering with Bootstrap

I've been attempting to create a filtering gallery for the past three days without any success. None of the codes I came across, or wrote myself, seem to be working. I am using the bootstrap 4 starter template that comes with the latest JQuery version, so I'm puzzled as to why it's not functioning properly. The JavaScript file is also linked to the HTML.

HTML

<section id="projects" class="padding">
        <div class="row">
            <div class="d-flex justify-content-center col-12">
                <div class="projects-filter">
                    <button class="btn btn-outline current" data-filter="*">All</button>
                    <button class="btn btn-outline" data-filter="featured">Featured</button>
                    <button class="btn btn-outline" data-filter="windmills">Windmills</button>
                    <button class="btn btn-outline" data-filter="equipment">Equipment</button>
                    <button class="btn btn-outline" data-filter="craftsmanship">Craftsmanship</button>
                </div>
            </div>
        </div>
        <div class="row">
            <div class="projects-content d-flex justify-content-center">
                <div class="single-project col-4 windmills">
                    <div class="single-project-image">
                        <img src="img/projects/alutechnika01.jpg" class="img-fluid">
                    </div>
                    <div class="single-project-desc">
                        <h5>Lorem ipsum</h5>
                        <p>Lorem ipsum dolor sit amet</p>
                    </div>
                </div>
                <div class="single-project col-4 featured">
                    <div class="single-project-image">
                        <img src="img/projects/alutechnika01.jpg" class="img-fluid">
                    </div>
                    <div class="single-project-desc">
                        <h5>Lorem ipsum</h5>
                        <p>Lorem ipsum dolor sit amet</p>
                    </div>
                </div>
                <div class="single-project col-4 windmills">
                    <div class="single-project-image">
                        <img src="img/projects/alutechnika01.jpg" class="img-fluid">
                    </div>
                    <div class="single-project-desc">
                        <h5>Lorem ipsum</h5>
                        <p>Lorem ipsum dolor sit amet</p>
                    </div>
                </div>
            </div>
        </div>
    </section>

Javascript:

$(window).load(function(){
var $container = $('.projects-content');
$container.isotope({
    filter: '*',
    animationOptions: {
        duration: 750,
        easing: 'linear',
        queue: false
    }
});

$('.projects-filter button').click(function(){
    $('.projects-filter .current').removeClass('current');
    $(this).addClass('current');

    var selector = $(this).attr('data-filter');
    $container.isotope({
        filter: selector,
        animationOptions: {
            duration: 750,
            easing: 'linear',
            queue: false
        }
     });
     return false;
    }); 
});

Answer №1

To convert the filter:selector to a className, simply add a . before the selector. For more information, check out this resource - .

var $container = $('.projects-content');
$container.isotope({
  filter: '*',
  animationOptions: {
    duration: 750,
    easing: 'linear',
    queue: false
  }
});

$('.projects-filter button').click(function() {
  $('.projects-filter .current').removeClass('current');
  $(this).addClass('current');

  var selector = $(this).attr('data-filter');

  $container.isotope({
    filter: (selector == '*') ? '*' : '.' + selector,
    animationOptions: {
      duration: 750,
      easing: 'linear',
      queue: false
    }
  });
  return false;
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://unpkg.com/isotope-layout@3/dist/isotope.pkgd.min.js"></script>
<section id="projects" class="padding">
  <div class="row">
    <div class="d-flex justify-content-center col-12">
      <div class="projects-filter">
        <button class="btn btn-outline current" data-filter="*">All</button>
        <button class="btn btn-outline" data-filter="featured">Featured</button>
        <button class="btn btn-outline" data-filter="windmills">Windmills</button>
        <button class="btn btn-outline" data-filter="equipment">Equipment</button>
        <button class="btn btn-outline" data-filter="crafts">Crafts</button>
      </div>
    </div>
  </div>
  <div class="row">
    <div class="projects-content d-flex justify-content-center">
      <div class="single-project col-4 windmills">
        <div class="single-project-image">
          <img src="img/projects/alutechnika01.jpg" class="img-fluid">
        </div>
        <div class="single-project-desc">
          <h5>Lorem ipsum</h5>
          <p>Lorem ipsum dolor sit amet</p>
        </div>
      </div>
      <div class="single-project col-4 featured">
        <div class="single-project-image">
          <img src="img/projects/alutechnika01.jpg" class="img-fluid">
        </div>
        <div class="single-project-desc">
          <h5>Lorem ipsum</h5>
          <p>Lorem ipsum dolor sit amet</p>
        </div>
      </div>
      <div class="single-project col-4 windmills">
        <div class="single-project-image">
          <img src="img/projects/alutechnika01.jpg" class="img-fluid">
        </div>
        <div class="single-project-desc">
          <h5>Lorem ipsum</h5>
          <p>Lorem ipsum dolor sit amet</p>
        </div>
      </div>
    </div>
  </div>
</section>

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

What is the best way to determine which button was clicked in Django?

My HTML has two buttons - one for accepting and the other for rejecting requests. I am trying to figure out how to determine which button was pressed along with the email of the applicant (tutor_application.tutor.chalkslateuser.email). Despite multiple a ...

My React application came to an unexpected halt with an error message stating: "TypeError: Cannot read properties of undefined (reading 'prototype')"

Everything was running smoothly with my app until I encountered this error without making any significant changes: TypeError: Cannot read properties of undefined (reading 'prototype') (anonymous function) .../client/node_modules/express/lib/resp ...

Creating a customizable ASP.NET UserControl with placeholder for HTML content

Although this question may have been asked before, I was unable to find any references to it. My apologies if it appears to be a duplicate question. My goal is to develop a versatile DialogBox as an ASP.NET UserControl, which would include all the necessa ...

How can one effectively implement private methods in React JS for optimal performance?

When implementing a component or element callback for an event, tutorials and documentation typically provide code examples like this: 'use strict'; import React from 'react'; let FooComponent = React.createClass({ handleClick(args) ...

Difficulty switching back and forth between three varying heights

I have a container with a button labeled "Show more". Upon clicking the button, the height of the container will transition through 3 different states. <div class="segment-suggestion-content height-small"> <div class="segment-suggestion-sh ...

Develop a fresh button using either JavaScript or PHP

I have designed a mini cart (drop down) on my WordPress site with different styles for when there are products and when empty. To enhance the design, I have utilized CSS pseudo elements before and after to add content dynamically. Is it possible to includ ...

Updating ng-model value in AngularJS controller does does not reflect changes in selection

I'm encountering an issue while trying to upgrade my ng-model within a selection feature. Here is the HTML code I am currently using: <div ng-app> <div ng-controller="Ctrl"> <select ng-model="viewmodel.inputDevice" ...

Having trouble retrieving JSON data from an API using jQuery's AJAX method

Currently, I am attempting to retrieve a JSON object from an API endpoint using jQuery and AJAX. Here is the code that I have been working with: $.ajax({ url: 'https://this-specific-website.com/customers.json', type: 'GET', ...

Ways to display additional information in typeahead using Angular JS

Currently, I am using the Bootstrap directory typeahead in Angular. I am looking to display more results in my HTML template instead of just the name: typeahead="job as job.name for job in getJobPlace($viewValue) | filter:{name:$viewValue}" I would like ...

Utilizing previously written HTML code snippets

While working on a page within a legacy application, I find myself repeatedly reusing a large HTML block of code. The entire HTML and JavaScript codebase is quite old. The specific HTML block in question spans over 200 lines of code. My current strategy in ...

Is there a way to skip using a temporary variable when using array.from?

let list = []; Array.from({ length: 1 }).forEach(() => { list.push({ value: Math.floor(Math.random() * 10) }); }); console.log(list) I implemented the code above to create an array of objects. However, I used 'list' as a temporary va ...

What could be the reason why the module is not being detected in Angular's TestBed?

Could you please explain why the module is not found in Angular? Here is my code: https://codesandbox.io/s/8k7vlvl5q2 The issue seems to be on this line: <<import { ComponentFixture, async, TestBed } from "@angular/testing"; >> TestBed canno ...

Angular 8: How to Retrieve Query Parameters from Request URL

Can I retrieve the GET URL Query String Parameters from a specific URL using my Angular Service? For example, let's say I have a URL = "http:localhost/?id=123&name=abc"; or URL = ""; // in my service.ts public myFunction(): Observale<any> ...

Is it possible to apply a slanted CSS3 line-through effect on text?

Can CSS3 be used to create a slanted strike-through line over text, starting from the bottom-left and going to the top-right in a diagonal direction? ...

Refresh Material-Ui's Selection Options

Is there a way to properly re-render the <option> </option> inside a Material UI select component? My goal is to transfer data from one object array to another using the Material UI select feature. {transferData.map(data => ( <option ...

Search field in DataTables appears to be misaligned

I'm in the process of developing a small website using JSP and DataTables (currently only for the first table). Here's what I have so far: As you can observe, there seems to be an alignment issue with the search field position. I'm n ...

When attempting to execute my code using "ts-node index.ts" in Visual Studio Code, the error "formatDiagnostics is not a function" is displayed, indicating an issue with the function call

/usr/local/lib/node_modules/ts-node/src/index.ts:857 const diagnosticText = formatDiagnostics(diagnostics, diagnosticHost); TypeError: formatDiagnostics is not defined at createTSError (/usr/local/lib/node_modules/ts-node/src/index.ts ...

PHP scheduler alternative

I have a PHP script called updater.php that performs a task for 1-2 minutes. It is crucial for the script to complete its job. While I can schedule it with cron, I have come up with an alternative solution. The script should only run at specific times wh ...

When attempting to transfer code from my local environment to Google Action/Dialogflow, no progress is made

I'm currently working on creating a Google Action in Dialogflow. Specifically, I am trying to deploy JavaScript code from my terminal. A few days ago, everything was running smoothly. Here is the index.js file that was working: 'use strict&apos ...

Present an error message via AJAX if the action is unauthorized in Laravel 5.1

When checking a policy in a controller, it can be done like this: $this->authorize('user', $post); In the Laravel 5.1 documentation, it states: If the action is authorized, the controller will continue executing normally; however, if the au ...