The script ceased functioning immediately following the inclusion of a case-insensitive search feature and interactive images

In the process of creating my inaugural project featuring a collection of images, I wanted to include a filter/search bar at the top. This bar would dynamically filter the displayed pictures based on user input. For example, typing "Aatrox" into the search bar should only display images related to "Aatrox" and not any other unrelated images like "Jayce" or "Senna". The script initially worked fine; I even added a .toLowerCase() function to make it case-insensitive. Additionally, I made the pictures clickable so that each image directs the user to its own respective page. However, after implementing these changes, the search functionality ceased to work.

Below is a snippet of the JavaScript code:

        <script>
            function search(){
                var searchText = (document.getElementById("searchInput").value).toLowerCase();
                var images = document.querySelectorAll(".image_container > img");
          
                if(searchText.length > 0){
                    images.forEach((image) => {
                        image.classList.add("hide");
                        if((image.dataset.tags).toLowerCase().indexOf(searchText) > -1){
                            image.classList.remove("hide");
                        }
                    });
                }else{
                    images.forEach((image) => {
                        image.classList.remove("hide");
                    });
                }
            }
        </script>

Below is the HTML structure:


<head>
        <title> Counterpicks </title>
        <meta charset="utf-8">
        <link rel="stylesheet" href="style.css">
    </head>

    <body>
        <h1> Counterpicks pro debily </h1>
        <div class="container">
            <div class="searchbox_container">
                <div class="searchbox">
                    <input type="text" name=" " placeholder="Search" class="search" id="searchInput" onkeyup="search()">
                </div>
            </div>


         <div class="image_container">
            <a href="aatrox.html"><img data-tags="aatrox" src="aatrox.webp" alt="Aatrox" class="actionimages"></a>
            <a href="ahri.html"><img data-tags="ahri" src="ahri.webp" alt="Ahri" class="actionimages"></a>
         </div>

Some lines have been omitted for brevity as they are repeated throughout the code.

Here is an excerpt from the CSS file:

.container {
    background: rgba(0,0,0,0);
    text-align: center;
    margin-left: 20%;
    margin-right: 20%;
}

.searchbox {
    text-align: center;
    margin-left: 20%;
    margin-right: 20%;
    margin-bottom: 20px;
  }
  
.image_container {
    clear:both;
}
  
  
.hide {
  display:none;

This being my first project involving JavaScript, I am open to any constructive feedback or criticism.

Answer №1

Swap out:

let pictures = document.querySelectorAll(".picture_holder > img");

swap in:

let pictures = document.querySelectorAll(".picture_holder > a > img");

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

Most efficient method for comparing two JSON arrays and rearranging their positions

I am faced with a challenge involving two Javascript JSON arrays. The arrays in question are named this.BicyclePartsOLD and this.BicyclePartsNEW. Both arrays contain an attribute named "ListOrder". The OLD array is currently ordered from ListOrder 1 to n ...

Passing PHP array to JavaScript in a specific format

After running the code provided, I find that the data returned is in an array format but unfortunately not easily referenced. AJAX: $.ajax({ url: './FILE.php', type: 'post', data: {'action': 'allfolders'}, ...

Run a PHP function using <button onclick=""> tag

Is it possible to trigger the execution of a PHP script when clicking an HTML button? I am aware that simply calling a PHP function directly from the button's onclick event like this: <button onclick="myPhpFunction("testString")">Button</butt ...

the live binding feature fails to function properly once an append operation is executed

Could someone please explain to me why adding an even number of squares causes the bind event to stop working? $("#add").click(function(){ $("#container").append("<div class=\"square\">xxxxxx</div> ").bind("click",function(e) { ...

After reaching the character limit, errors occur due to data being sent through Ajax requests on keyup

My website has a feature where users can input zip codes and get information based on that. However, I am facing an issue with the ajax call when users continue typing beyond the required number of characters for zip code entry. Even though the additional ...

Interactive Autocomplete Component

I am encountering issues with passing dynamic data to my autocomplete angularjs directive, which is built using jQuery-UI autocomplete. Below is the current code I am working with: HTML: <div ng-app="peopleApp"> <div ng-controller="indexCont ...

Is it possible to apply two ngClass directives to a single div element in Angular 4?

When I click to start editing, a component is called for the editing process. In this component, I am unable to click on anything and the background turns black, which is okay. However, I would like for each ID that I select to edit to become active with a ...

React: Dynamic input field that removes default value as the user begins typing

Imagine a scenario where we have a text box in a React application with Formik and Material UI that accepts a price as a floating point number. By default, the field is set to 0. However, once the user manually enters a number, the default value should be ...

What steps should I take to ensure that my Ngrok domain is functioning properly?

Screenshot of Endpoints I've been facing challenges while attempting to get my NGROK domain operational. Despite completing all the necessary setup steps and successfully running multiple tunnels simultaneously with NSSM, as well as having a business ...

Preventing Text Chaos in CSS Layouts: Tips and Tricks

I'm facing an issue with the alignment on this page: . When the page is resized, some of the text links are too long and causing a problem with the layout of the stacked images. How can I fix this? Here is the HTML & CSS code for reference: CSS: #b ...

Bring in React components using a specific namespace

Here is the structure of my React component: /build .............................. /lib /inner /InnerComponent.js /index.js /OuterComponent1.js /OuterComponent2.js /index.js ................................. package.jso ...

I'm trying to display hidden forms on a webpage when a button is clicked using the DojoToolkit, but I'm having trouble figuring out what's going wrong with my code

Currently, I am trying to grasp the concepts of Dojotoolkit and my objective is to display a form when a button is clicked. Upon reviewing other examples, my code seems correct to me; however, there appears to be something crucial that I am overlooking but ...

In React conditional return, it is anticipated that there will be a property assignment

What is the optimal way to organize a conditional block that relies on the loggedIn status? I am encountering an issue with a Parsing error and unexpected token. Can someone help me identify what mistake I am making and suggest a more efficient approach? ...

Tips for Customizing the StrongLoop LoopBack Explorer CSS

Our team is currently utilizing Strongloop's LoopBack framework for our REST APIs and we are looking to customize the CSS of the LoopBack Explorer. Unfortunately, it seems unclear which CSS files are being employed (LoopBack vs Swagger) and their exac ...

[entity: undefined prototype] { type: 'clip', info: 'Watch my latest video!!' } using nodejs - multer

import routes from "./routes"; import multer from "multer"; const multerVideo = multer({ dest: "videos/" }); export const localsMiddleware = (req, res, next) => { res.locals.siteName = "Webtube"; res.locals.routes = routes; res.locals.user = { isA ...

Encountering difficulties in accessing files displayed by serve-index in Express

My Node.js server using Express seems to be working fine for displaying directory contents, but I'm running into an issue when trying to access individual files. After clicking on a file listed in the directory, I keep getting an error message that sa ...

Guide on utilizing Vue to trigger an API call when the input box loses focus

I am currently facing challenges while trying to learn vue, and I am not sure how to proceed. I would greatly appreciate any assistance! To begin with, I want to acknowledge that my English may not be perfect, but I will do my best to explain my issue tho ...

"Efficiently handle online submissions: Use Django to submit a form and instantly display the outcome

I have an urgent task to develop a web page where users can input data, and the backend will perform calculations and display the result below the input field on the same page (similar to checking online air ticket prices). I am new to Django and HTML. Be ...

Is there a way for me to connect the ajax data to Vue components?

Utilizing Jquery ajax to load data from an external API has been successful and the v-for text is also working without any issues. Vue var vm = new Vue({ el:'.inlinePoetry', data:{ PoetryList:[] }, created:function(){ var ...

The form submission did not yield any results

function calculateCost() { var projectorCost=0,price=0,quantity=0,discountPrice=0,totalCost=0; var modelName=document.getElementById('projectormodel').value; var quantity=document.getElementById('q ...