Tips on resetting the position of a div after filtering out N other divs

Check out this code snippet.

HTML Code: X.html

<input type="text" id="search-criteria"/>
<input type="button" id="search" value="search"/>
  <div class="col-sm-3">
    <div class="misc">
      <div class="box box-info">
        <div class="box-header with-border">
           <h3 class="box-title">Services</h3>
        </div>
      </div>
    </div>
  </div>
  <div class="col-sm-3">
    <div class="misc">
      <div class="box box-warning">
        <div class="box-header with-border">
           <h3 class="box-title">Branches</h3>
        </div>
      </div>
    </div>
  </div>
  <div class="col-sm-3">
    <div class="misc">
      <div class="box box-danger">
        <div class="box-header with-border">
           <h3 class="box-title">Firm Type</h3>
        </div>
      </div>
    </div>
  </div>
  <div class="col-sm-3">
    <div class="misc">
      <div class="box box-success">
        <div class="box-header with-border">
           <h3 class="box-title">Designations</h3>
        </div>
      </div>
    </div>
  </div>

Javascript Code: X.js

$('#search-criteria').keyup(function(){
    $('.misc').hide();
    var regex = new RegExp($('#search-criteria').val(), 'i');
    $('.misc').filter(function() {
       return regex.test($(this).text());
    }).show();
});

I have implemented the above code and it works fine. However, I am facing an issue where the position of the displayed div is not refreshed to the top of the page. There are several divs with the class misc. After searching for a specific content, the matching div is visible but stays in its original position. I would like the div's position to reset to the beginning of the page once a search is performed. Additionally, when the search input is cleared, all divs should be visible as before.

Here is an image depicting the current behavior:

No Search:

https://i.sstatic.net/aVfiq.png

After Search:

https://i.sstatic.net/rF5SN.png

Expected Output:

https://i.sstatic.net/qCDPp.png

Answer №1

The divs inside your column layout are only being hidden, not the columns themselves. To achieve the desired effect, you need to hide the col-sm-3 divs.

Answer №2

To conceal the columns instead of the elements inside them, simply apply the parent() method to the .misc elements. Follow this example:

$('#search-criteria').keyup(function(){
    var regex = new RegExp($('#search-criteria').val(), 'i');
    var elements$ = $('.misc').parent();
    elements$.hide().filter(function() {
       return regex.test($(this).text());
    }).show();
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<div>
<input type="text" id="search-criteria"/>
<input type="button" id="search" value="search"/>
</div>
  <div class="col-sm-3">
    <div class="misc">
      <div class="box box-info">
        <div class="box-header with-border">
           <h3 class="box-title">Services</h3>
        </div>
      </div>
    </div>
  </div>
  <div class="col-sm-3">
    <div class="misc">
      <div class="box box-warning">
        <div class="box-header with-border">
           <h3 class="box-title">Branches</h3>
        </div>
      </div>
    </div>
  </div>
  <div class="col-sm-3">
    <div class="misc">
      <div class="box box-danger">
        <div class="box-header with-border">
           <h3 class="box-title">Firm Type</h3>
        </div>
      </div>
    </div>
  </div>
  <div class="col-sm-3">
    <div class="misc">
      <div class="box box-success">
        <div class="box-header with-border">
           <h3 class="box-title">Designations</h3>
        </div>
      </div>
    </div>
  </div>

Answer №3

consider changing the structure to something like this

<div class="misc">
 <div class="col-sm-3">
   <div class="box box-info">
     <div class="box-header with-border">
       <h3 class="box-title">Services</h3>
     </div>
   </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

What are the best practices for implementing optional chaining in object data while using JavaScript?

In my current project, I am extracting singlePost data from Redux and converting it into an array using Object.keys method. The issue arises when the rendering process is ongoing because the singlePost data is received with a delay. As a result, the initi ...

Extract information from one class and pass it to another class's data-value using jQuery

Currently, I am encountering an issue with JQuery data retrieval directly from the User. My goal is to extract the value from one class and assign it to another class as data-value. Here is a brief outline of my structure - https://i.sstatic.net/kEatS.jpg ...

Utilize JavaScript to extract and exhibit various SQL records stored within a multidimensional array

How can I use JS, JQuery, PHP, and MySQLi to automatically generate an HTML table within a div on a webpage? The table should display data based on user-input search criteria. For example, the user enters a start date and end date, clicks a button, which t ...

Graphics distorting on android devices when implementing createjs and canvas technology

I have a question regarding the performance of my game that I'm developing to work on both desktop browsers and mobile devices. Using HTML5 and CreateJs, most of the game is being drawn on a canvas element. While everything runs smoothly on a standar ...

Covering a doughnut shape with a twisting spiral

I want to achieve a hula hoop covered in tape effect using three.js. The 3D model should look similar to the image below. https://i.sstatic.net/lj9cR.jpg I have been able to create the hoop in 3D space using TorusGeometry and pan around, but I am strugg ...

Tips for retrieving multiple data outputs from an ajax success function

Within my project, I have two separate JavaScript files named myJs1.js and myJs2.js. One of the methods in myJs1.js is invoking a method from myJs2.js. My goal is to retrieve the values r1 and r2 into the results (within myJs1.js). I attempted to achiev ...

Javascript increasing the variable

Whenever I interact with the code below, it initially displays locationsgohere as empty. However, upon a second click, the data appears as expected. For example, if I input London, UK in the textarea with the ID #id, the corresponding output should be var ...

Trigger a popup notification with JavaScript when

Check out this snippet of code: <link rel="stylesheet" href="https://code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css"> <script src="https://code.jquery.com/jquery-1.12.4.js"></script> <script src="https://code.jquery.com/ui/ ...

Pass the value of a variable from one component to another component by utilizing ngModel

I have a scenario where I am working with two components named first and second. The second component is calling the first component. Within the first component, there is a module called matslider that toggles on and off. My goal is to retrieve the status ...

The body of the POST request appears to be void of any

Whenever I make a request using curl or hurl, an issue arises. Despite req.headers['content-length'] showing the correct length and req.headers['content-type'] being accurate, req.body returns as {}. Below is the Hurl test: POST http:/ ...

What could be causing the regex to fail when trying to validate the header in a request?

Utilizing regex to enhance the response header, I am referring to this set of instructions: https://nextjs.org/docs/api-reference/next.config.js/headers The documentation explains how to incorporate Regex Path Matching, however, it seems to be ineffectiv ...

Centering the logo using Material-UI's alignment feature

Need help centering a logo in my login form using Material-UI. Everything else is centered except for the logo, which is stuck to the left side of the card. I've tried adding align="center" and justify="center" under the img tag, but it's still ...

Display the text and values of textareas/inputs located within a designated section of a form

I am looking to extract the labels and values of specific textareas and input tags on my website. I don't want to capture all of them, just those within a particular area. For example, I only want to retrieve the values that come after the textarea wi ...

Is there a way to ensure that the text inside the red div is fully contained within the div, so that when it is expanded, all the text will be displayed properly?

I've been experimenting with creating an interactive function that expands a red box and reveals additional text when the cursor hovers over it, using a vertical linear transition. Unfortunately, I'm facing difficulty in implementing this feature ...

Dealing with an overflow of Redis clients in NextJS: Simplifying the process with just one client

Introduction Hello there! I have developed a new app for a discord web-dashboard and utilized redis as my database. The main Issue Whenever I send an axios request to one of the documents in the api folder, which requires a redis client, a new redis cl ...

Tips for building and implementing Angular URL Parameters for URLs in the form: "component/#/?id=..."

I am currently facing a situation where I have an application with an existing user base. I am looking to avoid disrupting their current links for a smoother transition. However, the previous links are in this format: (server)/viewer/#/?id=12. Please see t ...

Plunker fails to run simple AngularJS demo

I'm having trouble figuring out why this basic example isn't functioning as expected on Plunker. http://plnkr.co/edit/EfNxzzQhAb8xAcFZGKm3?p=preview var app = angular.module("App",[]); var Controller = function($scope){ $scope.message ="Hel ...

Incorrect data retrieval from JSON object with JQUERY

I am working on a function that extracts data from a database on the server-side and assigns it as the value of a textbox on the client-side. The function is functioning correctly on the server-side, but on the client-side, when I use console.log to view t ...

Guide to invoking an API in Next.js 13 by utilizing specific variables within a client component

I currently have a collection of products that are accessible on my website through a straightforward function within a server component. async function getData() { const res = await fetch(`${apiPath}`); const data = (await res.json()) as PackProps ...

Aligning the title vertically in Ionic on Android device

Currently, I am in the process of developing an application using the Ionic framework and cordova. After uploading my app for testing purposes, I encountered a small issue. The title in the top bar on Android appears to be misaligned vertically. Is there a ...