Challenge with Filter Functionality when Activating Button

Can you help me implement a search filter using buttons with the Isotope Plugin?

For example, entering a search value in an input field and then clicking a search button to display the search results. How can I achieve this using buttons? Below is the HTML and JS code snippets along with a Codepen link for reference. View Codepen link

$(function() {

  var $grid = $('#container');
  $grid.isotope({
    itemSelector: '.item'
  });

  var filters = [];  
    
  //Button-Search  
  $('#btn').on('click', function() {
      filters[0] = this.value;
      runFilter();
  });

  $('#filter-select').on('change', function() {
    filters[1] = this.value;
    runFilter();
  });
 
  var runFilter = function() {
    $grid.isotope({
      filter: function() {
        if (filters[0]) {
          var qsRegex = new RegExp(filters[0], 'gi');
          if (!$(this).find('.content-title').text().match(qsRegex)) {
            return false;
          }
        }
        if (filters[1]) {
          if (!($(this).hasClass(filters[1]))) {
            return false;
          }
        }
        return true;
      }
    });
  }
});
<input type="text" id="search" placeholder="Search"></input>
<button type="submit" id="btn">Click Me</button>

Answer №1

Update your search button function with this revised code. Instead of using this.value, make sure to retrieve the value from the input field.

//Button-Search  
$('#btn').on('click', function() {
    filters[0] = $('#search')[0].value; // Update this line
    runFilter();
});

Check out the updated code on Codepen here

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

"Learn how to handle exceptions in Nest JS when checking for existing users in MongoDB and creating a new user if the user does

Below is the implementation of the addUser method in users.service.ts - // Function to add a single user async addUser(createUserDTO: CreateUserDTO): Promise<User> { const newUser = await this.userModel(createUserDTO); return newUser.save() ...

What is the best way to position the hamburger menu icon on the right side of the header?

I need some assistance with adjusting the position of my mobile hamburger menu icon. It's currently on the left side of the screen, but I want to move it to the right. I've tried making the changes myself using CSS and HTML, but I'm not an e ...

Missing pieces of data | Utilizing React and Redux Toolkit

I'm facing a problem that's keeping me up for almost 24 hours. I just finished coding this slice, but when I submit the data, only the automatically generated ID is returned. Let me explain further - I have a skill component with two input forms ...

Tips for incorporating a local image using file:// in JavaFX WebKit

I am facing an issue with displaying a picture in a JavaFX webview using local HTML. Despite writing the code, I am unable to show the image. The URL of the image is "file:/D:/a.jpg". //Main WebViewBrowser.java public class WebViewBrowser extends Appli ...

Capturing user input by detecting the Enter key using jQuery

I am brand new to coding and struggling with capturing user input when the enter key is pressed. Currently, I have a submit button that successfully captures user input upon clicking. <div> <div class="text-area"> <li class=" ...

Choose a text input form field simultaneously?

Is it possible to create a select field that also acts as an input form simultaneously? I need the options in this hybrid field to range from 0.01 to 10.00, while ensuring it always displays 2 decimal places. Curious how I can achieve this functionality ...

Utilizing commas in JavaScript

Hi everyone, I'm having an issue with printing the message "Invalid password, Must Contain:". The problem I'm facing is that when I write the code in JavaScript, the comma is being interpreted as an operator instead of a regular English comma. Th ...

Ways to update the text alongside a slider form with JavaScript

I am currently working on a project using html and js function slide(){ let v= document.getElementById("slide_inner"); document.getElementById("slider").textContent=v.value; } <form id="slider"> <label for="slide_inner"&g ...

Exploring the Power of NPM Modules in an Electron Renderer

Having trouble accessing lodash in an electron renderer. I'm a beginner with Electron and know that both the main process and renderer (local html file) have access to node. I can require something from node core like fs and it works fine, but when I ...

Should GIT be utilized for managing version control of HTML pages?

Currently, I am developing an application that automatically generates blog pages in HTML format using JSON files. In addition to this functionality, the app also needs to support versioning for each blog created. The process involves converting JSON dat ...

Displaying images in a horizontal row instead of a vertical column when using ng-repeat

I am currently using ng-repeat to showcase a series of images. While I believe this may be related to a CSS matter, I am uncertain. <div ng-repeat="hex in hexRow track by hex.id"> <img ng-src="{{hex.img}}"/> </div> How can I arrange ...

Managing user access and permissions using JavaScript on the front end

I find myself in a situation where I am working on developing a single page application: The majority of the operations rely on ajax requests. Depending on the user's login status (admin, regular user, visitor), different components need to be displ ...

What is the best way to control the overflow length and display only a specific amount of content at once?

I am currently designing the homepage for a social media platform. I have around 45 dummy posts and I am looking for a way to limit the overflow in CSS so that only 5 posts are displayed at a time. Once the user reaches the bottom of those 5 posts, another ...

Slippery carousel: Showcase all items in center alignment mode

Currently, I am implementing Slick Carousel with centerMode. Is there a method to ensure that all items are displayed without being cut off on the screen? The setup I have is quite simple: $('.your-class').slick({ centerMode: true, slidesTo ...

Spin a three-dimensional cube on a platform using three.js with a unique functionality

I am attempting to create an animation featuring a cube on a flat surface. The cube can be rotated along the x/y axis only, with no need to show its underside. I want to be able to tip the cube over any edge - when a side of the cube touches the surface, i ...

Embedding PHP code within inline styles

Is it possible to incorporate a variable inside my progress bar? I want the progress to change based on the status of each project ($projek), but inserting the variable directly into the style attribute doesn't seem to be working. Here's the code ...

Charting Add-Ons for jQuery

Looking for suggestions on user-friendly graph drawing plugins compatible with jQuery. Currently developing a web application that will retrieve data from a remote database and present it through visual graphs... Explored jgcharts (the jQuery Google Chart ...

JavaScript: a single function and two calls to Document.getElementById() results in both returning "undefined"

Within my JavaScript file, I have a function that utilizes two variables: choice and courseChosen. The latter variable must be converted into an object first. In the HTML, the tags courseName and courseInfo are used for choice and courseChosen respectively ...

Verifying the selection state of a dynamically generated checkbox with JavaScript

I have a table with checkboxes. Each time a button is clicked, I dynamically add new checkboxes to the table like so: var cell3 = row.insertCell(2); cell3.innerHTML = '<input type="checkBox" value=\"selected?\" style="cursor:poin ...

What is the best method for sending a JavaScript variable to the server and back again?

I'm currently working on a JavaScript project where I need to build a string. Let's say, for the sake of this example: var cereal = 'my awesome string'; There's also a button on my webpage: <button id="send" type="submit" nam ...