A lightweight JavaScript library that adds dynamic search functionality to any web page with minimal setup.
- Simple Setup: Just add HTML attributes to your elements
- Zero Dependencies: Pure vanilla JavaScript, no jQuery or other libraries needed
- Real-time Filtering: Updates as the user types
- Tag Support: Filter by custom tags
- Responsive: Works on all devices and screen sizes
- No Configuration: Works out of the box
Download the latest release and include it in your project:
<script src="path/to/dropsearch.min.js"></script>- Add the script to your HTML:
<script src="path/to/dropsearch.min.js"></script>- Add the
dropsearch-inputattribute to your search input:
<input type="text" dropsearch-input="products" placeholder="Search...">- Add the
dropsearchattribute to elements you want to filter:
<div dropsearch="products">Product One</div>
<div dropsearch="products">Product Two</div>
<div dropsearch="products">Product Three</div>- That's it! The library initializes automatically.
An example implementation is included in the example/index.html file. This example demonstrates:
- Basic implementation with Bootstrap styling
- Product catalog with multiple filterable items
- Tag-based filtering for more precise searches
- Responsive layout for all device sizes
- "No results found" message that appears when no matches are found
To run the example:
- Open the
example/index.htmlfile in your browser - Try typing in the search box to see real-time filtering
- Search for product names or tag keywords (e.g. "headphones", "electronics", "books")
Add tags to your items for more precise filtering:
<div dropsearch="products" dropsearch-tags="electronics,gadgets">Smartphone</div>
<div dropsearch="products" dropsearch-tags="clothing,fashion">T-Shirt</div>You can have multiple independent search groups on the same page:
<!-- Products Search -->
<input type="text" dropsearch-input="products" placeholder="Search products...">
<div dropsearch="products">Product One</div>
<div dropsearch="products">Product Two</div>
<!-- Users Search -->
<input type="text" dropsearch-input="users" placeholder="Search users...">
<div dropsearch="users">User One</div>
<div dropsearch="users">User Two</div>If you dynamically add elements to your page, you can re-initialize the library:
window.initDropSearch();<div id="no-results" style="display: none;">
No results found
</div>
<script>
document.addEventListener('DOMContentLoaded', () => {
const searchInput = document.querySelector('[dropsearch-input="products"]');
const items = document.querySelectorAll('[dropsearch="products"]');
const noResults = document.getElementById('no-results');
searchInput.addEventListener('input', () => {
setTimeout(() => {
const visibleItems = Array.from(items).filter(item =>
item.style.display !== 'none'
);
noResults.style.display = visibleItems.length === 0 ? 'block' : 'none';
}, 100);
});
});
</script>| Attribute | Description | Example |
|---|---|---|
dropsearch-input="id" |
Add to input elements to create a search field | <input dropsearch-input="products"> |
dropsearch="id" |
Add to elements that should be filtered | <div dropsearch="products">Item</div> |
dropsearch-tags="tag1,tag2" |
Add comma-separated tags for additional filtering | <div dropsearch="products" dropsearch-tags="clothing,red">Red Shirt</div> |
| Method | Description |
|---|---|
window.initDropSearch() |
Re-initializes the library (use after dynamically adding elements) |
DropSearchJS works in all modern browsers:
- Chrome (latest)
- Firefox (latest)
- Safari (latest)
- Edge (latest)
- Opera (latest)
- Chrome for Android (latest)
- Safari iOS (latest)
DropSearchJS is designed to be extremely lightweight and performant:
- No external dependencies
- Uses efficient DOM selectors
- Minimal reflows and repaints
If you need to customize how matches are determined, you can extend the library:
// Save the original prototype method
const originalFilterItems = DropSearch.prototype.filterItems;
// Override with custom logic
DropSearch.prototype.filterItems = function(searchId, query) {
// Your custom implementation here
// or call the original with custom behavior
originalFilterItems.call(this, searchId, query);
}Download the source code and use your preferred method to minify the JavaScript file for production use.
Contributions are welcome. Please submit a Pull Request.
- Fork the repository
- Create your feature branch (
git checkout -b feature/feature-name) - Commit your changes (
git commit -m 'Add feature') - Push to the branch (
git push origin feature/feature-name) - Open a Pull Request
This project is licensed under the MIT License - see the LICENSE file for details.