Dynamic search bar in Javascript showing empty spaces for cards that are not currently visible

Currently, I am in the process of developing an online learning platform which features a catalog of courses. Users can search for courses based on the titles or descriptions using the search bar. To display the catalog, I have utilized bootstrap cards. The functionality works smoothly on mobile devices, with the results appearing vertically one after another. However, on desktop, I am encountering an issue where hidden cards result in empty spaces and the displayed results have large gaps between them. This creates a disjointed user experience. Is there a way to rectify this issue on desktop while maintaining the current layout on mobile? Thank you!

Click here to view the "empty spaces"

JavaScript code:

$('#live_search').on('keyup', function() {
    let input = $(this).val();
    $('.card').hide();
    $('.card').filter(function() {
        return new RegExp(input, 'i').test($(this).text())
    }).show();
});

Cards:

<div class="search">
            <form>
                <label for="live_search">Search for a course :</label>
                <input type="text" id="live_search" size="30" class="form-control" placeholder="Search for a course" />
            </form>
        </div>
        <br>
            <div class="container px-5 mb-3">
                <div class="row">
                    {% for formation in formations %}
                        <div class="col-md-4">
                            <div class="card">
                                <img class="card-img-top" src="{{ vich_uploader_asset(formation, 'imageFile') }}" alt="{{ formation.imageName }}">
                                <div class="card-body">
                                    <h5 class="card-title">{{ formation.title }}</h5>
                                    <p class="card-text">{{ formation.description }}</p>
                                    <a class="small btn btn-success custom-btn rounded-pill px-3" href="{{path('app_formations_see', {'id' : formation.id })}}" role="button">view</a>
                                    <span class="small">by {{ formation.user.firstname }} {{ formation.user.lastname }}</span>
                                </div>
                            </div>
                        </div>
                    {% endfor %}
                </div>
            </div>

Answer №1

Conceal the card while maintaining the col-md-4 division. It is possible to combine the col-md-4 and card divisions.

Answer №2

To hide the col-md-4 division, you can create a "wrapper" class and use it in your code.

JavaScript Code:

$('#live_search').on('keyup', function() {
    let input = $(this).val();
    $('.wrapper').hide();
    $('.wrapper').filter(function() {
        return new RegExp(input, 'i').test($(this).text())
    }).show();
});

Card Display:

<div class="search">
            <form>
                <label for="live_search">Search for a course:</label>
                <input type="text" id="live_search" size="30" class="form-control" placeholder="Search for a course" />
            </form>
        </div>
        <br>
            <div class="container px-5 mb-3">
                <div class="row">
                    {% for formation in formations %}
                        <div class="col-md-4 wrapper">
                            <div class="card">
                                <img class="card-img-top" src="{{ vich_uploader_asset(formation, 'imageFile') }}" alt="{{ formation.imageName }}">
                                <div class="card-body">
                                    <h5 class="card-title">{{ formation.title }}</h5>
                                    <p class="card-text">{{ formation.description }}</p>
                                    <a class="small btn btn-success custom-btn rounded-pill px-3" href="{{path('app_formations_see', {'id' : formation.id })}}" role="button">view</a>
                                    <span class="small">by {{ formation.user.firstname }} {{ formation.user.lastname }}</span>
                                </div>
                            </div>
                        </div>
                    {% endfor %}
                </div>
            </div>

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

Achieving a Pushing Footer Design with Content

Hey guys, I'm working on a website and I'm having some trouble with the footer. It's sticking to the bottom of the page, but the content is overflowing it. Check out this screenshot for reference: . Here is my HTML/CSS code for the footer: ...

File not found: The specified file 'C:Self Project eact-shopper eact-shopperclientuildindex.html' does not exist

I followed the tutorial and startup code by Reed Barger exactly, but every time I try to run the server I encounter this error: Error: ENOENT: no such file or directory, stat 'C:\Self Project\react-shopper\react-shopper\client&bso ...

Personalize rejection message in the context of Promise.all()

Hello, I am currently working on customizing the error response in case a promise from an array fails. After referencing Handling errors in Promise.all, I have come up with the following code. However, I may need to make some adjustments to achieve the de ...

How to Create a Custom Callback Function for jQuery's .html

I am currently working with the following code: $.ajax({ type: 'GET', url: 'index.php?route=checkout/onepagecheckout/getpaypaldata', dataType: 'json', success: function(json) { ...

What is the best way to expand a div in a downward direction exclusively?

My task involves a parent div containing a centered child div. My goal is to have the child div grow downwards only upon clicking it, not in both directions. Initial state of the two divs: https://i.sstatic.net/cWYyV.png Outcome after clicking on div 2: ...

Maximizing Bootstrap card size in Angular4: A step-by-step guide

How can I make Bootstrap cards resizable on top of other elements when clicked on an icon to make it full screen and overlay on other cards? detail.component.html <div class="card card-outline-info" > <div class="card-header bg-info"><h5 ...

angular8StylePreprocessorSettings

I'm currently trying to implement the approach found on this tutorial in order to import scss files through stylePreprocessorOptions in Angular 8. However, I'm encountering an error stating that the file cannot be found. Any suggestions on how to ...

Error: FileReader is not defined in Node.js (Nest.js) environment

I am working on converting my image to base64 using a custom function. However, when I try to execute the code, I encounter an error message stating ReferenceError: FileReader is not defined. This error has left me puzzled and unsure of its cause. Below i ...

Getting the Mongoose Model using Express parameters is a simple process

Is it possible to dynamically fetch a mongoose model based on the req.params.model? Check out this example of a Schema const mongoose = require("mongoose"); const Schema = mongoose.Schema; const SmartSchema = new Schema({ SmartPriority: { type: Stri ...

"Troubleshooting: jQuery Find function not functioning correctly with HTML template

I am having trouble with a Shopify liquid code that I am trying to load into an HTML template <script type="text/template" id="description"> <div class="product-ddd"> {{ product.description }} </div> ...

The beforeunload function in jQuery is not behaving as anticipated

My current goal is to show a pop-up with three choices whenever a user attempts to close the tab while on my website, and then store that choice somewhere. In my main.js file, which loads across all site pages, I have implemented the following code: $(do ...

Struggling to delete a specific item by its ID from MongoDB within a Next.js application

Currently, I am building a todo app in nextjs to enhance my skills. However, I'm encountering some difficulties in deleting single todos from the database using the deleteOne function. Below is the frontend call: async function deleteTodo(id) { a ...

Issue encountered while constructing an array within the component data following the 'created' event

It's been a while since I've dealt with Vue.Js, so my memory is a bit fuzzy. I'm facing an issue where I can't seem to modify a data array within a function that is called in the created hook. My goal is to create a multidimensional ar ...

difficulty with parsing JSON in jQuery when referencing an external file containing the same data

Looking for help with parsing a json file using jquery? Check out this working sample: http://jsfiddle.net/bw85zeea/ I'm encountering an issue when trying to load "data2" from an external file. The browser keeps complaining that it's an invalid ...

Exploring Ngu-Carousel in Angular 7: Importing JSON data for a dynamic display

After attempting to import data from JSON and display it using ngu-carousel, I encountered an error. The error shows "length of undefined" Subsequently, when I try to click on the previous/next button, another error appears. This error states "innerHTML ...

Cannot see the box-shadow with :before and z-index in CSS

My main objective is to prevent the box-shadow from overlapping with adjacent elements by utilizing the :before pseudo-element and adjusting the z-index. However, I am facing an issue where the shadow appears beneath the container of the list item that is ...

Retrieving information from database with the help of PHP and AJAX

I am having trouble printing data from a PHP and Ajax request that is initially encoded into JSON. Instead of displaying the data properly on the screen, it is showing elements from four rows all in a single line with commas separating them. $(document ...

How can I set a default radio button selection when the page is loaded?

Input your radio button code below: <ion-radio ng-model="data.gender" ng-required="true" ng-value="'male'">Male</ion-radio> <ion-radio ng-model="data.gender" ng-required="true" ng-value="'female'">Female</ion-rad ...

Utilize YII's Render Partial method with a specified time interval for dynamic content

I have a requirement to automatically update the content of a div when refreshing the entire page. To achieve this, I used Ajax to renderPartial in YII. Currently, I have implemented it using an AJAX button on click. Below is my code: <?php ech ...

Changes in query parameters on NextJS navigation within the same page do not activate hooks

When utilizing NextJS without SSR, I encountered an issue with basic navigation using different query parameters. Upon the initial arrival on the page/component, everything seems fine as the component gets mounted and URL params change accordingly. However ...