Categories: Engineering

$.search – jQuery plugin for search on HTML elements

Reading Time: 3 mins

$.search is a jQuery plugin that lets you hide or show matching elements as the user types in a search box.
In this article, you will learn how to:

  1. create a search experience that highlights the matching elements in a list.
  2. enable search transformations on input text to do fuzzy/synonym searches

Live Demo: Here’s what you will end up building:

  • Open up your Terminal of choice and clone the demo project using below command
  • $ git clone https://github.com/tejesh95/g1-search.git
  • Create an HTML file index.html
  • Include jQuery and g1 js library files using the CDN link and instantiate search module using $('body').search()

<!DOCTYPE html>
<html>
<head>
  <title>g1 search</title>
</head>
<body>
  <script src="https://cdn.jsdelivr.net/combine/npm/jquery,npm/g1"></script>
  <script>
    $('body').search()
  </script>
</body>
</html>
Add the HTML elements that need to toggle their visibility based on text in search input
On search input, set data-target attribute to .list-item because list <li> elements have to be identified.
To search text-content of list-item elements, set the data-search attribute to @text

Create a class called d-none in <style> section inside <head> tag

<!DOCTYPE html>
<html>
<head>
  <title>g1 search</title>
  <style>
    .d-none {
      display: none;
    }
  </style>
</head>
Set data-hide-class="d-none" attribute to add the d-none class to all non-matching target elements.


To summarise: On <input type="search"> tag,

  • data-target attribute is to identify target elements.
  • data-search attribute is to identify text content inside the target elements.
  • data-hide-class attribute adds the class to all non-matching target elements.

The final code looks like:

<!DOCTYPE html>
<html>
<head>
  <title>g1 search</title>
  <style>
    .d-none {
      display: none;
    }
  </style>
</head>
<body>
  <input type="search" data-search="@text" data-target=".list-item" data-hide-class="d-none">
  <ul>
    <li class="list-item">First item text content</li>
    <li class="list-item">Second item text content</li>
    <li class="list-item">Third item text content</li>
  </ul>
  <script src="https://cdn.jsdelivr.net/combine/npm/jquery,npm/g1"></script>
  <script>
    $('body').search()
  </script>

</body>
</html>

A look into another example:

To hide elements that do not match url link attribute

To find matches based on href  attribute url links, give data-search=”href”.

Similarly, to find matches based on title attribute values, give data-search=”title”

To read full documentation of $.search, head to https://learn.gramener.com/guide/g1/search

In part 2 of the article, we will explore how to enable search transformations on input text to do fuzzy/synonym searches.

Tejesh P

Leave a Comment
Share
Published by
Tejesh P

Recent Posts

The 10 Hottest Computer Vision Trends Shaping 2025

Computer vision (CV) has become an essential platform in the rapidly changing technology. It is… Read More

7 months ago

Top 5 Benefits of Visual Inspection Systems You Must not Ignore

Are you struggling with lengthy and labour-intensive processes of manual inspection at your manufacturing? You… Read More

7 months ago

AI for Inventory Optimization – Maximizing Efficiency and Minimizing Costs

Managing smarter inventory is always challenging: too much stock consumes money, while too little results… Read More

8 months ago

How Does Computer Vision Help With Managing Perishable Goods?

The global food industry faces significant losses daily due to the spoilage of perishable goods.… Read More

9 months ago

Enhancing Warehouse Accuracy with Computer Vision

In today’s fast-paced world of e-commerce and supply chain logistics, warehouses are more than just… Read More

9 months ago

How AI is Redefining Quality Control and Supercharging OEE Optimization?

What does it mean to redefine the future of manufacturing with AI? At the heart… Read More

10 months ago

This website uses cookies.