AJAX search improvements
August 7, 2025 Β· AI Conversations
<style>
.insta-search-results{display:none;z-index:999999;background-color:#fff;position:absolute;right:0;width:100%;border:none;border-bottom:1px solid #cacaca}
@media (min-width:768px){.insta-search-results{width:600px;border:1px solid #cacaca;border-top:none}}
@media (min-width:981px){.insta-search-results{position:absolute;right:152px}}
.insta-search-results li{width:100%;display:inline-block;list-style:none;}
.search-listing{padding:3px 10px;}
.search-product{vertical-align:top;}
.insta-search-results img{float:left;margin-right:5px;}
.search-listing a{text-transform:uppercase;font-size:13px;display:block;min-height:60px;color:#666;}
.search-listing .amount, .search-listing .woocommerce-Price-currencySymbol{color:#666;}
.search-name{padding:0;font-weight:700;line-height:normal}
.insta-search-results .woocommerce-Price-amount.amount{font-weight:700}
.search-heading{text-transform:uppercase;background-color:#f8f8f8;color:#999;font-weight:700;font-size:12px;padding:0 10px;border:1px solid #eaeaea}
.search-footer{margin:0;}
.search-footer a {color:#999;}
.search-listing:hover{background: #f2f2f2;}
.search-listing:hover a,.search-listing:hover .amount,.search-listing:hover .woocommerce-Price-currencySymbol {color: #08c;}
</style>
<script>
jQuery(document).ready(function($) {
function setResultsPos() {
//var scrollPos = $(window).scrollTop();
var searchPos = $('.et_search_form_container').offset().top + $('.et_search_form_container').outerHeight();
//var resultsPos = searchPos + scrollPos;
$('.insta-search-results').css("top", searchPos + "px");
}
//When the search box is clicked, set the position of the results element
$('.et-search-field').on('click', function(){
setResultsPos();
});
//Set position of the results element on first keypress
$('.et-search-field').one('keyup', function(){
setResultsPos();
});
$(window).on('resize', function(){
setResultsPos();
});
//Close search results when the close search X button is clicked
$('.et_close_search_field').on('click', function(){
if ( $('.insta-search-results').is(':visible') ) {
$('.insta-search-results').slideUp();
}
});
//Close search results on clicking away from search or results element
$(document).on('click', function(event){
if ( !$(event.target).closest('.insta-search-results').length && !$(event.target).closest('.et-search-form').length ) {
$('.insta-search-results').slideUp();
}
});
$('.et-search-field').on('keyup', function(){
//empty the results
$('.insta-search-results').html('');
if ( $(this).val() !== '' && $(this).val().length > 1 ) {
//get the search term, and lowercase it
var searchTerm = $(this).val().toLowerCase();
//construct regex
var reSearch = new RegExp('\b' + searchTerm, 'gi');
var foundSomething = false;
var foundProducts = 0;
var foundCats = 0;
//loop through the products and search
$('#pmm-instant-search li').each(function(){
var searchData = $(this).data('search').toLowerCase();
if ( searchData.search(reSearch) > -1 ) {
foundSomething = true;
if ( $(this).data('type') === 'product' ) {
foundProducts++;
if ( foundProducts > 4 ) {
return true;
}
} else {
foundCats++;
if ( foundCats > 4 ) {
return true;
}
}
var displayProduct = $(this).clone();
var imgUrl = displayProduct.data('image-url');
var prodUrl = displayProduct.data('product-url');
var prodDiv = displayProduct.children('.search-product');
prodDiv.prepend('<img src="' + imgUrl + '" width="60" height="60">');
prodDiv.wrap('<a href="' + prodUrl + '"></a>');
displayProduct.appendTo('.insta-search-results');
}
});
if ( foundSomething ) {
if ( foundProducts ) {
$('.insta-search-results').prepend('<li class="search-heading">Products</li>');
}
if ( foundCats ) {
$('.search-listing[data-type="category"]').eq(0).before('<li class="search-heading">Categories</li>');
}
$('.insta-search-results').append('<li class="search-heading search-footer"><a href="#" onclick="jQuery('.et-search-form').submit();">See More Results</a></li>');
} else {
$('.insta-search-results').html('<li style="padding:10px;">Sorry, no results found.</li>');
}
if ( !$('.insta-search-results').is(':visible') ) {
$('.insta-search-results').slideDown();
}
} else {
setTimeout(function(){
if ( $('.et-search-field').val().length < 2 ) {
$('.insta-search-results').slideUp();
}
}, 50);
}
})
});
</script>
<style>
.insta-search-results{display:none;z-index:999999;background-color:#fff;position:absolute;right:0;width:100%;border:none;border-bottom:1px solid #cacaca}
@media (min-width:768px){.insta-search-results{width:600px;border:1px solid #cacaca;border-top:none}}
@media (min-width:981px){.insta-search-results{position:absolute;right:152px}}
.insta-search-results li{width:100%;display:inline-block;list-style:none;}
.search-listing{padding:3px 10px;}
.search-product{vertical-align:top;}
.insta-search-results img{float:left;margin-right:5px;}
.search-listing a{text-transform:uppercase;font-size:13px;display:block;min-height:60px;color:#666;}
.search-listing .amount, .search-listing .woocommerce-Price-currencySymbol{color:#666;}
.search-name{padding:0;font-weight:700;line-height:normal}
.insta-search-results .woocommerce-Price-amount.amount{font-weight:700}
.search-heading{text-transform:uppercase;background-color:#f8f8f8;color:#999;font-weight:700;font-size:12px;padding:0 10px;border:1px solid #eaeaea}
.search-footer{margin:0;}
.search-footer a {color:#999;}
.search-listing:hover{background: #f2f2f2;}
.search-listing:hover a,.search-listing:hover .amount,.search-listing:hover .woocommerce-Price-currencySymbol {color: #08c;}
</style>
<script>
jQuery(document).ready(function($) {
function setResultsPos() {
//var scrollPos = $(window).scrollTop();
var searchPos = $('.et_search_form_container').offset().top + $('.et_search_form_container').outerHeight();
//var resultsPos = searchPos + scrollPos;
$('.insta-search-results').css("top", searchPos + "px");
}
//When the search box is clicked, set the position of the results element
$('.et-search-field').on('click', function(){
setResultsPos();
});
//Set position of the results element on first keypress
$('.et-search-field').one('keyup', function(){
setResultsPos();
});
$(window).on('resize', function(){
setResultsPos();
});
//Close search results when the close search X button is clicked
$('.et_close_search_field').on('click', function(){
if ( $('.insta-search-results').is(':visible') ) {
$('.insta-search-results').slideUp();
}
});
//Close search results on clicking away from search or results element
$(document).on('click', function(event){
if ( !$(event.target).closest('.insta-search-results').length && !$(event.target).closest('.et-search-form').length ) {
$('.insta-search-results').slideUp();
}
});
$('.et-search-field').on('keyup', function(){
//empty the results
$('.insta-search-results').html('');
if ( $(this).val() !== '' && $(this).val().length > 1 ) {
//get the search term, and lowercase it
var searchTerm = $(this).val().toLowerCase();
//construct regex
var reSearch = new RegExp('\b' + searchTerm, 'gi');
var foundSomething = false;
var foundProducts = 0;
var foundCats = 0;
//loop through the products and search
$('#pmm-instant-search li').each(function(){
var searchData = $(this).data('search').toLowerCase();
if ( searchData.search(reSearch) > -1 ) {
foundSomething = true;
if ( $(this).data('type') === 'product' ) {
foundProducts++;
if ( foundProducts > 4 ) {
return true;
}
} else {
foundCats++;
if ( foundCats > 4 ) {
return true;
}
}
var displayProduct = $(this).clone();
var imgUrl = displayProduct.data('image-url');
var prodUrl = displayProduct.data('product-url');
var prodDiv = displayProduct.children('.search-product');
prodDiv.prepend('<img src="' + imgUrl + '" width="60" height="60">');
prodDiv.wrap('<a href="' + prodUrl + '"></a>');
displayProduct.appendTo('.insta-search-results');
}
});
if ( foundSomething ) {
if ( foundProducts ) {
$('.insta-search-results').prepend('<li class="search-heading">Products</li>');
}
if ( foundCats ) {
$('.search-listing[data-type="category"]').eq(0).before('<li class="search-heading">Categories</li>');
}
$('.insta-search-results').append('<li class="search-heading search-footer"><a href="#" onclick="jQuery('.et-search-form').submit();">See More Results</a></li>');
} else {
$('.insta-search-results').html('<li style="padding:10px;">1Sorry, no results found.</li>');
}
if ( !$('.insta-search-results').is(':visible') ) {
$('.insta-search-results').slideDown();
}
} else {
setTimeout(function(){
if ( $('.et-search-field').val().length < 2 ) {
$('.insta-search-results').slideUp();
}
}, 50);
}
})
});
</script>
<script>
jQuery(document).ready(function($) {
var data = {
action: 'load_serchdata',
//whatever: 1234
};
jQuery.post(ajaxurl, data, function(response) {
jQuery('#pmm-instant-search').html(response);
});
});
</script>
—
### β Step-by-step solution
#### 1. **Track if AJAX data is loaded**
You need a flag to determine whether the search data (`#pmm-instant-search`) is loaded via the AJAX call.
Add this at the **top of your “ block** (below `jQuery(document).ready(function($) {`):
let isSearchDataLoaded = false;
—
#### 2. **Set flag when AJAX completes**
In your existing AJAX `jQuery.post(…)`, after setting the HTML, update the flag:
jQuery.post(ajaxurl, data, function(response) {
jQuery('#pmm-instant-search').html(response);
isSearchDataLoaded = true;
});
—
#### 3. **Update "keyup" handler to show "Searching…"**
Update the main `keyup` event logic inside:
$('.et-search-field').on('keyup', function(){
$('.insta-search-results').html('');
if ($(this).val() !== '' && $(this).val().length > 1) {
const searchTerm = $(this).val().toLowerCase();
const reSearch = new RegExp('\b' + searchTerm, 'gi');
let foundSomething = false;
let foundProducts = 0;
let foundCats = 0;
if (!isSearchDataLoaded) {
$('.insta-search-results').html('<li style="padding:10px;">Searching...</li>').slideDown();
return;
}
$('#pmm-instant-search li').each(function() {
const searchData = $(this).data('search').toLowerCase();
if (searchData.search(reSearch) > -1) {
foundSomething = true;
if ($(this).data('type') === 'product') {
foundProducts++;
if (foundProducts > 4) return true;
} else {
foundCats++;
if (foundCats > 4) return true;
}
const displayProduct = $(this).clone();
const imgUrl = displayProduct.data('image-url');
const prodUrl = displayProduct.data('product-url');
const prodDiv = displayProduct.children('.search-product');
prodDiv.prepend('<img src="' + imgUrl + '" width="60" height="60">');
prodDiv.wrap('<a href="' + prodUrl + '"></a>');
displayProduct.appendTo('.insta-search-results');
}
});
if (foundSomething) {
if (foundProducts) {
$('.insta-search-results').prepend('<li class="search-heading">Products</li>');
}
if (foundCats) {
$('.search-listing[data-type="category"]').eq(0).before('<li class="search-heading">Categories</li>');
}
$('.insta-search-results').append('<li class="search-heading search-footer"><a href="#" onclick="jQuery('.et-search-form').submit();">See More Results</a></li>');
} else {
$('.insta-search-results').html('<li style="padding:10px;">Sorry, no results found.</li>');
}
if (!$('.insta-search-results').is(':visible')) {
$('.insta-search-results').slideDown();
}
} else {
setTimeout(function() {
if ($('.et-search-field').val().length < 2) {
$('.insta-search-results').slideUp();
}
}, 50);
}
});
—
### β Result
– π While AJAX data hasnβt loaded: shows **"Searching…"**.
– β
Once loaded:
– If matches are found β show results.
– If not β show **"Sorry, no results found."**
—
Let me know if you want a **loader icon** next to the `"Searching…"` text or want to **disable typing until AJAX completes**.
—
### β
1. **Declare the flag**
**π Location:** Inside `jQuery(document).ready(function($) {`
**π Line to insert:** **Line ~1347**, right after `jQuery(document).ready(function($) {`
let isSearchDataLoaded = false;
—
### β
2. **Update AJAX callback**
**π Location:** Inside the second “ block (near end)
**π Line to update:** **Line ~1470** β in this part:
Change this:
jQuery('#pmm-instant-search').html(response);
To:
jQuery('#pmm-instant-search').html(response);
isSearchDataLoaded = true;
—
### β
3. **Modify `.et-search-field` `keyup` handler**
**π Location:** First “ block, where `$('.et-search-field').on('keyup', function(){` starts
**π Starts at:** **Line ~1365**
**π§© Apply inside the keyup handler:**
Add **this condition right after**:
if ($(this).val() !== '' && $(this).val().length > 1) {
Insert at **Line ~1369**:
if (!isSearchDataLoaded) {
$('.insta-search-results').html('<li style="padding:10px;">Searching...</li>').slideDown();
return;
}
—
### π§ Summary of Changes
| Line # (approx) | Action | Code |
|—————-|—————————————-|——|
| 1347 | Add variable declaration | `let isSearchDataLoaded = false;` |
| 1369 | Add AJAX not-loaded condition inside `keyup` | `if (!isSearchDataLoaded) {…}` |
| 1470 | Update AJAX callback to set flag | `isSearchDataLoaded = true;` |
Let me know if you want me to paste the **full updated “ block** for easier copy-paste.
<style>
.insta-search-results{display:none;z-index:999999;background-color:#fff;position:absolute;right:0;width:100%;border:none;border-bottom:1px solid #cacaca}
@media (min-width:768px){.insta-search-results{width:600px;border:1px solid #cacaca;border-top:none}}
@media (min-width:981px){.insta-search-results{position:absolute;right:152px}}
.insta-search-results li{width:100%;display:inline-block;list-style:none;}
.search-listing{padding:3px 10px;}
.search-product{vertical-align:top;}
.insta-search-results img{float:left;margin-right:5px;}
.search-listing a{text-transform:uppercase;font-size:13px;display:block;min-height:60px;color:#666;}
.search-listing .amount, .search-listing .woocommerce-Price-currencySymbol{color:#666;}
.search-name{padding:0;font-weight:700;line-height:normal}
.insta-search-results .woocommerce-Price-amount.amount{font-weight:700}
.search-heading{text-transform:uppercase;background-color:#f8f8f8;color:#999;font-weight:700;font-size:12px;padding:0 10px;border:1px solid #eaeaea}
.search-footer{margin:0;}
.search-footer a {color:#999;}
.search-listing:hover{background: #f2f2f2;}
.search-listing:hover a,.search-listing:hover .amount,.search-listing:hover .woocommerce-Price-currencySymbol {color: #08c;}
</style>
<script>
jQuery(document).ready(function($) {
function setResultsPos() {
//var scrollPos = $(window).scrollTop();
var searchPos = $('.et_search_form_container').offset().top + $('.et_search_form_container').outerHeight();
//var resultsPos = searchPos + scrollPos;
$('.insta-search-results').css("top", searchPos + "px");
}
//When the search box is clicked, set the position of the results element
$('.et-search-field').on('click', function(){
setResultsPos();
});
//Set position of the results element on first keypress
$('.et-search-field').one('keyup', function(){
setResultsPos();
});
$(window).on('resize', function(){
setResultsPos();
});
//Close search results when the close search X button is clicked
$('.et_close_search_field').on('click', function(){
if ( $('.insta-search-results').is(':visible') ) {
$('.insta-search-results').slideUp();
}
});
//Close search results on clicking away from search or results element
$(document).on('click', function(event){
if ( !$(event.target).closest('.insta-search-results').length && !$(event.target).closest('.et-search-form').length ) {
$('.insta-search-results').slideUp();
}
});
$('.et-search-field').on('keyup', function(){
//empty the results
$('.insta-search-results').html('');
if ( $(this).val() !== '' && $(this).val().length > 1 ) {
//get the search term, and lowercase it
var searchTerm = $(this).val().toLowerCase();
//construct regex
var reSearch = new RegExp('\b' + searchTerm, 'gi');
var foundSomething = false;
var foundProducts = 0;
var foundCats = 0;
//loop through the products and search
$('#pmm-instant-search li').each(function(){
var searchData = $(this).data('search').toLowerCase();
if ( searchData.search(reSearch) > -1 ) {
foundSomething = true;
if ( $(this).data('type') === 'product' ) {
foundProducts++;
if ( foundProducts > 4 ) {
return true;
}
} else {
foundCats++;
if ( foundCats > 4 ) {
return true;
}
}
var displayProduct = $(this).clone();
var imgUrl = displayProduct.data('image-url');
var prodUrl = displayProduct.data('product-url');
var prodDiv = displayProduct.children('.search-product');
prodDiv.prepend('<img src="' + imgUrl + '" width="60" height="60">');
prodDiv.wrap('<a href="' + prodUrl + '"></a>');
displayProduct.appendTo('.insta-search-results');
}
});
if ( foundSomething ) {
if ( foundProducts ) {
$('.insta-search-results').prepend('<li class="search-heading">Products</li>');
}
if ( foundCats ) {
$('.search-listing[data-type="category"]').eq(0).before('<li class="search-heading">Categories</li>');
}
$('.insta-search-results').append('<li class="search-heading search-footer"><a href="#" onclick="jQuery('.et-search-form').submit();">See More Results</a></li>');
} else {
$('.insta-search-results').html('<li style="padding:10px;">Sorry, no results found.</li>');
}
if ( !$('.insta-search-results').is(':visible') ) {
$('.insta-search-results').slideDown();
}
} else {
setTimeout(function(){
if ( $('.et-search-field').val().length < 2 ) {
$('.insta-search-results').slideUp();
}
}, 50);
}
})
});
</script>
<style>
.insta-search-results{display:none;z-index:999999;background-color:#fff;position:absolute;right:0;width:100%;border:none;border-bottom:1px solid #cacaca}
@media (min-width:768px){.insta-search-results{width:600px;border:1px solid #cacaca;border-top:none}}
@media (min-width:981px){.insta-search-results{position:absolute;right:152px}}
.insta-search-results li{width:100%;display:inline-block;list-style:none;}
.search-listing{padding:3px 10px;}
.search-product{vertical-align:top;}
.insta-search-results img{float:left;margin-right:5px;}
.search-listing a{text-transform:uppercase;font-size:13px;display:block;min-height:60px;color:#666;}
.search-listing .amount, .search-listing .woocommerce-Price-currencySymbol{color:#666;}
.search-name{padding:0;font-weight:700;line-height:normal}
.insta-search-results .woocommerce-Price-amount.amount{font-weight:700}
.search-heading{text-transform:uppercase;background-color:#f8f8f8;color:#999;font-weight:700;font-size:12px;padding:0 10px;border:1px solid #eaeaea}
.search-footer{margin:0;}
.search-footer a {color:#999;}
.search-listing:hover{background: #f2f2f2;}
.search-listing:hover a,.search-listing:hover .amount,.search-listing:hover .woocommerce-Price-currencySymbol {color: #08c;}
</style>
<script>
jQuery(document).ready(function($) {
function setResultsPos() {
//var scrollPos = $(window).scrollTop();
var searchPos = $('.et_search_form_container').offset().top + $('.et_search_form_container').outerHeight();
//var resultsPos = searchPos + scrollPos;
$('.insta-search-results').css("top", searchPos + "px");
}
//When the search box is clicked, set the position of the results element
$('.et-search-field').on('click', function(){
setResultsPos();
});
//Set position of the results element on first keypress
$('.et-search-field').one('keyup', function(){
setResultsPos();
});
$(window).on('resize', function(){
setResultsPos();
});
//Close search results when the close search X button is clicked
$('.et_close_search_field').on('click', function(){
if ( $('.insta-search-results').is(':visible') ) {
$('.insta-search-results').slideUp();
}
});
//Close search results on clicking away from search or results element
$(document).on('click', function(event){
if ( !$(event.target).closest('.insta-search-results').length && !$(event.target).closest('.et-search-form').length ) {
$('.insta-search-results').slideUp();
}
});
$('.et-search-field').on('keyup', function(){
//empty the results
$('.insta-search-results').html('');
if ( $(this).val() !== '' && $(this).val().length > 1 ) {
//get the search term, and lowercase it
var searchTerm = $(this).val().toLowerCase();
//construct regex
var reSearch = new RegExp('\b' + searchTerm, 'gi');
var foundSomething = false;
var foundProducts = 0;
var foundCats = 0;
//loop through the products and search
$('#pmm-instant-search li').each(function(){
var searchData = $(this).data('search').toLowerCase();
if ( searchData.search(reSearch) > -1 ) {
foundSomething = true;
if ( $(this).data('type') === 'product' ) {
foundProducts++;
if ( foundProducts > 4 ) {
return true;
}
} else {
foundCats++;
if ( foundCats > 4 ) {
return true;
}
}
var displayProduct = $(this).clone();
var imgUrl = displayProduct.data('image-url');
var prodUrl = displayProduct.data('product-url');
var prodDiv = displayProduct.children('.search-product');
prodDiv.prepend('<img src="' + imgUrl + '" width="60" height="60">');
prodDiv.wrap('<a href="' + prodUrl + '"></a>');
displayProduct.appendTo('.insta-search-results');
}
});
if ( foundSomething ) {
if ( foundProducts ) {
$('.insta-search-results').prepend('<li class="search-heading">Products</li>');
}
if ( foundCats ) {
$('.search-listing[data-type="category"]').eq(0).before('<li class="search-heading">Categories</li>');
}
$('.insta-search-results').append('<li class="search-heading search-footer"><a href="#" onclick="jQuery('.et-search-form').submit();">See More Results</a></li>');
} else {
$('.insta-search-results').html('<li style="padding:10px;">1Sorry, no results found.</li>');
}
if ( !$('.insta-search-results').is(':visible') ) {
$('.insta-search-results').slideDown();
}
} else {
setTimeout(function(){
if ( $('.et-search-field').val().length < 2 ) {
$('.insta-search-results').slideUp();
}
}, 50);
}
})
});
</script>
<script>
jQuery(document).ready(function($) {
var data = {
action: 'load_serchdata',
//whatever: 1234
};
jQuery.post(ajaxurl, data, function(response) {
jQuery('#pmm-instant-search').html(response);
});
});
</script>
Hereβs the **exact change with line numbers** based on the final “ in that function.
—
### β Step 1: Add the loading flag
**π Line ~1869**
**π Add after:** `jQuery(document).ready(function($) {`
let isSearchDataLoaded = false;
—
### β Step 2: Set flag when AJAX loads data
**π Line ~1881**
**π Replace:**
jQuery('#pmm-instant-search').html(response);
**β With:**
jQuery('#pmm-instant-search').html(response);
isSearchDataLoaded = true;
—
### β Step 3: Modify the `.et-search-field` keyup logic
**π Line ~1800** β inside:
$('.et-search-field').on('keyup', function(){
**π After this line:**
if ($(this).val() !== '' && $(this).val().length > 1) {
**β Add:**
if (!isSearchDataLoaded) {
$('.insta-search-results').html('<li style="padding:10px;">Searching...</li>').slideDown();
return;
}
—
### β Final Integration Preview
Hereβs the **final placement** of the new line inside the `keyup` block (around line 1800+):
$('.et-search-field').on('keyup', function(){
$('.insta-search-results').html('');
if ($(this).val() !== '' && $(this).val().length > 1) {
if (!isSearchDataLoaded) {
$('.insta-search-results').html('<li style="padding:10px;">Searching...</li>').slideDown();
return;
}
var searchTerm = $(this).val().toLowerCase();
...
—
### β Summary of Changes
| Step | Action | Line (approx) |
|——|————————————-|—————|
| 1 | Add `let isSearchDataLoaded = false;` | ~1869 |
| 2 | Set flag in AJAX callback | ~1881 |
| 3 | Show "Searching…" when data not loaded | ~1802 |
This solution ensures your users **see βSearchingβ¦β until data loads**, and only then the search attempts and optionally shows βSorry, no results found.β
Let me know if you want this logic **also added to the first script block** (`csis_load_insta_search_product_list()`), or if you only use the second one.
<style>
.insta-search-results{display:none;z-index:999999;background-color:#fff;position:absolute;right:0;width:100%;border:none;border-bottom:1px solid #cacaca}
@media (min-width:768px){.insta-search-results{width:600px;border:1px solid #cacaca;border-top:none}}
@media (min-width:981px){.insta-search-results{position:absolute;right:152px}}
.insta-search-results li{width:100%;display:inline-block;list-style:none;}
.search-listing{padding:3px 10px;}
.search-product{vertical-align:top;}
.insta-search-results img{float:left;margin-right:5px;}
.search-listing a{text-transform:uppercase;font-size:13px;display:block;min-height:60px;color:#666;}
.search-listing .amount, .search-listing .woocommerce-Price-currencySymbol{color:#666;}
.search-name{padding:0;font-weight:700;line-height:normal}
.insta-search-results .woocommerce-Price-amount.amount{font-weight:700}
.search-heading{text-transform:uppercase;background-color:#f8f8f8;color:#999;font-weight:700;font-size:12px;padding:0 10px;border:1px solid #eaeaea}
.search-footer{margin:0;}
.search-footer a {color:#999;}
.search-listing:hover{background: #f2f2f2;}
.search-listing:hover a,.search-listing:hover .amount,.search-listing:hover .woocommerce-Price-currencySymbol {color: #08c;}
</style>
<script>
jQuery(document).ready(function($) {
function setResultsPos() {
//var scrollPos = $(window).scrollTop();
var searchPos = $('.et_search_form_container').offset().top + $('.et_search_form_container').outerHeight();
//var resultsPos = searchPos + scrollPos;
$('.insta-search-results').css("top", searchPos + "px");
}
//When the search box is clicked, set the position of the results element
$('.et-search-field').on('click', function(){
setResultsPos();
});
//Set position of the results element on first keypress
$('.et-search-field').one('keyup', function(){
setResultsPos();
});
$(window).on('resize', function(){
setResultsPos();
});
//Close search results when the close search X button is clicked
$('.et_close_search_field').on('click', function(){
if ( $('.insta-search-results').is(':visible') ) {
$('.insta-search-results').slideUp();
}
});
//Close search results on clicking away from search or results element
$(document).on('click', function(event){
if ( !$(event.target).closest('.insta-search-results').length && !$(event.target).closest('.et-search-form').length ) {
$('.insta-search-results').slideUp();
}
});
$('.et-search-field').on('keyup', function(){
//empty the results
$('.insta-search-results').html('');
if ( $(this).val() !== '' && $(this).val().length > 1 ) {
//get the search term, and lowercase it
var searchTerm = $(this).val().toLowerCase();
//construct regex
var reSearch = new RegExp('\b' + searchTerm, 'gi');
var foundSomething = false;
var foundProducts = 0;
var foundCats = 0;
//loop through the products and search
$('#pmm-instant-search li').each(function(){
var searchData = $(this).data('search').toLowerCase();
if ( searchData.search(reSearch) > -1 ) {
foundSomething = true;
if ( $(this).data('type') === 'product' ) {
foundProducts++;
if ( foundProducts > 4 ) {
return true;
}
} else {
foundCats++;
if ( foundCats > 4 ) {
return true;
}
}
var displayProduct = $(this).clone();
var imgUrl = displayProduct.data('image-url');
var prodUrl = displayProduct.data('product-url');
var prodDiv = displayProduct.children('.search-product');
prodDiv.prepend('<img src="' + imgUrl + '" width="60" height="60">');
prodDiv.wrap('<a href="' + prodUrl + '"></a>');
displayProduct.appendTo('.insta-search-results');
}
});
if ( foundSomething ) {
if ( foundProducts ) {
$('.insta-search-results').prepend('<li class="search-heading">Products</li>');
}
if ( foundCats ) {
$('.search-listing[data-type="category"]').eq(0).before('<li class="search-heading">Categories</li>');
}
$('.insta-search-results').append('<li class="search-heading search-footer"><a href="#" onclick="jQuery('.et-search-form').submit();">See More Results</a></li>');
} else {
$('.insta-search-results').html('<li style="padding:10px;">Sorry, no results found.</li>');
}
if ( !$('.insta-search-results').is(':visible') ) {
$('.insta-search-results').slideDown();
}
} else {
setTimeout(function(){
if ( $('.et-search-field').val().length < 2 ) {
$('.insta-search-results').slideUp();
}
}, 50);
}
})
});
</script>
<style>
.insta-search-results{display:none;z-index:999999;background-color:#fff;position:absolute;right:0;width:100%;border:none;border-bottom:1px solid #cacaca}
@media (min-width:768px){.insta-search-results{width:600px;border:1px solid #cacaca;border-top:none}}
@media (min-width:981px){.insta-search-results{position:absolute;right:152px}}
.insta-search-results li{width:100%;display:inline-block;list-style:none;}
.search-listing{padding:3px 10px;}
.search-product{vertical-align:top;}
.insta-search-results img{float:left;margin-right:5px;}
.search-listing a{text-transform:uppercase;font-size:13px;display:block;min-height:60px;color:#666;}
.search-listing .amount, .search-listing .woocommerce-Price-currencySymbol{color:#666;}
.search-name{padding:0;font-weight:700;line-height:normal}
.insta-search-results .woocommerce-Price-amount.amount{font-weight:700}
.search-heading{text-transform:uppercase;background-color:#f8f8f8;color:#999;font-weight:700;font-size:12px;padding:0 10px;border:1px solid #eaeaea}
.search-footer{margin:0;}
.search-footer a {color:#999;}
.search-listing:hover{background: #f2f2f2;}
.search-listing:hover a,.search-listing:hover .amount,.search-listing:hover .woocommerce-Price-currencySymbol {color: #08c;}
</style>
<script>
jQuery(document).ready(function($) {
function setResultsPos() {
//var scrollPos = $(window).scrollTop();
var searchPos = $('.et_search_form_container').offset().top + $('.et_search_form_container').outerHeight();
//var resultsPos = searchPos + scrollPos;
$('.insta-search-results').css("top", searchPos + "px");
}
//When the search box is clicked, set the position of the results element
$('.et-search-field').on('click', function(){
setResultsPos();
});
//Set position of the results element on first keypress
$('.et-search-field').one('keyup', function(){
setResultsPos();
});
$(window).on('resize', function(){
setResultsPos();
});
//Close search results when the close search X button is clicked
$('.et_close_search_field').on('click', function(){
if ( $('.insta-search-results').is(':visible') ) {
$('.insta-search-results').slideUp();
}
});
//Close search results on clicking away from search or results element
$(document).on('click', function(event){
if ( !$(event.target).closest('.insta-search-results').length && !$(event.target).closest('.et-search-form').length ) {
$('.insta-search-results').slideUp();
}
});
$('.et-search-field').on('keyup', function(){
//empty the results
$('.insta-search-results').html('');
if ( $(this).val() !== '' && $(this).val().length > 1 ) {
//get the search term, and lowercase it
var searchTerm = $(this).val().toLowerCase();
//construct regex
var reSearch = new RegExp('\b' + searchTerm, 'gi');
var foundSomething = false;
var foundProducts = 0;
var foundCats = 0;
//loop through the products and search
$('#pmm-instant-search li').each(function(){
var searchData = $(this).data('search').toLowerCase();
if ( searchData.search(reSearch) > -1 ) {
foundSomething = true;
if ( $(this).data('type') === 'product' ) {
foundProducts++;
if ( foundProducts > 4 ) {
return true;
}
} else {
foundCats++;
if ( foundCats > 4 ) {
return true;
}
}
var displayProduct = $(this).clone();
var imgUrl = displayProduct.data('image-url');
var prodUrl = displayProduct.data('product-url');
var prodDiv = displayProduct.children('.search-product');
prodDiv.prepend('<img src="' + imgUrl + '" width="60" height="60">');
prodDiv.wrap('<a href="' + prodUrl + '"></a>');
displayProduct.appendTo('.insta-search-results');
}
});
if ( foundSomething ) {
if ( foundProducts ) {
$('.insta-search-results').prepend('<li class="search-heading">Products</li>');
}
if ( foundCats ) {
$('.search-listing[data-type="category"]').eq(0).before('<li class="search-heading">Categories</li>');
}
$('.insta-search-results').append('<li class="search-heading search-footer"><a href="#" onclick="jQuery('.et-search-form').submit();">See More Results</a></li>');
} else {
$('.insta-search-results').html('<li style="padding:10px;">1Sorry, no results found.</li>');
}
if ( !$('.insta-search-results').is(':visible') ) {
$('.insta-search-results').slideDown();
}
} else {
setTimeout(function(){
if ( $('.et-search-field').val().length < 2 ) {
$('.insta-search-results').slideUp();
}
}, 50);
}
})
});
</script>
<script>
jQuery(document).ready(function($) {
var data = {
action: 'load_serchdata',
//whatever: 1234
};
jQuery.post(ajaxurl, data, function(response) {
jQuery('#pmm-instant-search').html(response);
});
});
</script>
"updates": [
{
"pattern": ".*",
"replacement": "
<script>
njQuery(document).ready(function($) {n let isSearchDataLoaded = false; // β
1. Add the flagnn function setResultsPos() {n var searchPos = $('.et_search_form_container').offset().top + $('.et_search_form_container').outerHeight();n $('.insta-search-results').css("top", searchPos + "px");n }nn $('.et-search-field').on('click', function(){n setResultsPos();n });nn $('.et-search-field').one('keyup', function(){n setResultsPos();n });nn $(window).on('resize', function(){n setResultsPos();n });nn $('.et_close_search_field').on('click', function(){n if ( $('.insta-search-results').is(':visible') ) {n $('.insta-search-results').slideUp();n }n });nn $(document).on('click', function(event){n if ( !$(event.target).closest('.insta-search-results').length && !$(event.target).closest('.et-search-form').length ) {n $('.insta-search-results').slideUp();n }n });nn $('.et-search-field').on('keyup', function(){n $('.insta-search-results').html('');nn if ($(this).val() !== '' && $(this).val().length > 1) {nn if (!isSearchDataLoaded) {n $('.insta-search-results').html('<li style="padding:10px;">Searching...</li>').slideDown();n return;n }nn var searchTerm = $(this).val().toLowerCase();n var reSearch = new RegExp('\\b' + searchTerm, 'gi');nn var foundSomething = false;n var foundProducts = 0;n var foundCats = 0;nn $('#pmm-instant-search li').each(function(){n var searchData = $(this).data('search').toLowerCase();n if ( searchData.search(reSearch) > -1 ) {n foundSomething = true;n if ( $(this).data('type') === 'product' ) {n foundProducts++;n if ( foundProducts > 4 ) return true;n } else {n foundCats++;n if ( foundCats > 4 ) return true;n }nn var displayProduct = $(this).clone();n var imgUrl = displayProduct.data('image-url');n var prodUrl = displayProduct.data('product-url');n var prodDiv = displayProduct.children('.search-product');n prodDiv.prepend('<img src="' + imgUrl + '" width="60" height="60">');n prodDiv.wrap('<a href="' + prodUrl + '"></a>');n displayProduct.appendTo('.insta-search-results');n }n });nn if ( foundSomething ) {n if ( foundProducts ) {n $('.insta-search-results').prepend('<li class="search-heading">Products</li>');n }n if ( foundCats ) {n $('.search-listing[data-type="category"]').eq(0).before('<li class="search-heading">Categories</li>');n }n $('.insta-search-results').append('<li class="search-heading search-footer"><a href="#" onclick="jQuery(\'.et-search-form\').submit();">See More Results</a></li>');n } else {n $('.insta-search-results').html('<li style="padding:10px;">Sorry, no results found.</li>');n }nn if ( !$('.insta-search-results').is(':visible') ) {n $('.insta-search-results').slideDown();n }n } else {n setTimeout(function(){n if ( $('.et-search-field').val().length < 2 ) {n $('.insta-search-results').slideUp();n }n }, 50);n }n });n});n
</script>
n
<script>
njQuery(document).ready(function($) {n var data = {n action: 'load_serchdata',n };n jQuery.post(ajaxurl, data, function(response) {n jQuery('#pmm-instant-search').html(response);n isSearchDataLoaded = true; // β
2. Set flag after loadingn });n});n
</script>
n
"name": "client_side_search",
"type": "code/php",
"content": "
<script>
njQuery(document).ready(function($) {n let isSearchDataLoaded = false; // u2705 1. Add the flagnn function setResultsPos() {n var searchPos = $('.et_search_form_container').offset().top + $('.et_search_form_container').outerHeight();n $('.insta-search-results').css("top", searchPos + "px");n }nn $('.et-search-field').on('click', function(){n setResultsPos();n });nn $('.et-search-field').one('keyup', function(){n setResultsPos();n });nn $(window).on('resize', function(){n setResultsPos();n });nn $('.et_close_search_field').on('click', function(){n if ( $('.insta-search-results').is(':visible') ) {n $('.insta-search-results').slideUp();n }n });nn $(document).on('click', function(event){n if ( !$(event.target).closest('.insta-search-results').length && !$(event.target).closest('.et-search-form').length ) {n $('.insta-search-results').slideUp();n }n });nn $('.et-search-field').on('keyup', function(){n $('.insta-search-results').html('');nn if ($(this).val() !== '' && $(this).val().length > 1) {nn if (!isSearchDataLoaded) {n $('.insta-search-results').html('<li style="padding:10px;">Searching...</li>').slideDown();n return;n }nn var searchTerm = $(this).val().toLowerCase();n var reSearch = new RegExp('\\b' + searchTerm, 'gi');nn var foundSomething = false;n var foundProducts = 0;n var foundCats = 0;nn $('#pmm-instant-search li').each(function(){n var searchData = $(this).data('search').toLowerCase();n if ( searchData.search(reSearch) > -1 ) {n foundSomething = true;n if ( $(this).data('type') === 'product' ) {n foundProducts++;n if ( foundProducts > 4 ) return true;n } else {n foundCats++;n if ( foundCats > 4 ) return true;n }nn var displayProduct = $(this).clone();n var imgUrl = displayProduct.data('image-url');n var prodUrl = displayProduct.data('product-url');n var prodDiv = displayProduct.children('.search-product');n prodDiv.prepend('<img src="' + imgUrl + '" width="60" height="60">');n prodDiv.wrap('<a href="' + prodUrl + '"></a>');n displayProduct.appendTo('.insta-search-results');n }n });nn if ( foundSomething ) {n if ( foundProducts ) {n $('.insta-search-results').prepend('<li class="search-heading">Products</li>');n }n if ( foundCats ) {n $('.search-listing[data-type="category"]').eq(0).before('<li class="search-heading">Categories</li>');n }n $('.insta-search-results').append('<li class="search-heading search-footer"><a href="#" onclick="jQuery(\'.et-search-form\').submit();">See More Results</a></li>');n } else {n $('.insta-search-results').html('<li style="padding:10px;">Sorry, no results found.</li>');n }nn if ( !$('.insta-search-results').is(':visible') ) {n $('.insta-search-results').slideDown();n }n } else {n setTimeout(function(){n if ( $('.et-search-field').val().length < 2 ) {n $('.insta-search-results').slideUp();n }n }, 50);n }n });n});n
</script>
n
<script>
njQuery(document).ready(function($) {n var data = {n action: 'load_serchdata',n };n jQuery.post(ajaxurl, data, function(response) {n jQuery('#pmm-instant-search').html(response);n isSearchDataLoaded = true; // u2705 2. Set flag after loadingn });n});n
</script>
"
}
– Show `"Searching…"` when the user types in the search field and data hasn't yet loaded via AJAX.
– Only display `"Sorry, no results found."` after data has loaded and no matches are found.
Let me know if you also want to:
– Add a loader icon beside the `"Searching…"` text,
– Prevent typing until data is loaded,
– Or apply this enhancement to the first script (`csis_load_insta_search_product_list`) as well.
<style>
.insta-search-results{display:none;z-index:999999;background-color:#fff;position:absolute;right:0;width:100%;border:none;border-bottom:1px solid #cacaca}
@media (min-width:768px){.insta-search-results{width:600px;border:1px solid #cacaca;border-top:none}}
@media (min-width:981px){.insta-search-results{position:absolute;right:152px}}
.insta-search-results li{width:100%;display:inline-block;list-style:none;}
.search-listing{padding:3px 10px;}
.search-product{vertical-align:top;}
.insta-search-results img{float:left;margin-right:5px;}
.search-listing a{text-transform:uppercase;font-size:13px;display:block;min-height:60px;color:#666;}
.search-listing .amount, .search-listing .woocommerce-Price-currencySymbol{color:#666;}
.search-name{padding:0;font-weight:700;line-height:normal}
.insta-search-results .woocommerce-Price-amount.amount{font-weight:700}
.search-heading{text-transform:uppercase;background-color:#f8f8f8;color:#999;font-weight:700;font-size:12px;padding:0 10px;border:1px solid #eaeaea}
.search-footer{margin:0;}
.search-footer a {color:#999;}
.search-listing:hover{background: #f2f2f2;}
.search-listing:hover a,.search-listing:hover .amount,.search-listing:hover .woocommerce-Price-currencySymbol {color: #08c;}
</style>
<script>
jQuery(document).ready(function($) {
function setResultsPos() {
//var scrollPos = $(window).scrollTop();
var searchPos = $('.et_search_form_container').offset().top + $('.et_search_form_container').outerHeight();
//var resultsPos = searchPos + scrollPos;
$('.insta-search-results').css("top", searchPos + "px");
}
//When the search box is clicked, set the position of the results element
$('.et-search-field').on('click', function(){
setResultsPos();
});
//Set position of the results element on first keypress
$('.et-search-field').one('keyup', function(){
setResultsPos();
});
$(window).on('resize', function(){
setResultsPos();
});
//Close search results when the close search X button is clicked
$('.et_close_search_field').on('click', function(){
if ( $('.insta-search-results').is(':visible') ) {
$('.insta-search-results').slideUp();
}
});
//Close search results on clicking away from search or results element
$(document).on('click', function(event){
if ( !$(event.target).closest('.insta-search-results').length && !$(event.target).closest('.et-search-form').length ) {
$('.insta-search-results').slideUp();
}
});
$('.et-search-field').on('keyup', function(){
//empty the results
$('.insta-search-results').html('');
if ( $(this).val() !== '' && $(this).val().length > 1 ) {
//get the search term, and lowercase it
var searchTerm = $(this).val().toLowerCase();
//construct regex
var reSearch = new RegExp('\b' + searchTerm, 'gi');
var foundSomething = false;
var foundProducts = 0;
var foundCats = 0;
//loop through the products and search
$('#pmm-instant-search li').each(function(){
var searchData = $(this).data('search').toLowerCase();
if ( searchData.search(reSearch) > -1 ) {
foundSomething = true;
if ( $(this).data('type') === 'product' ) {
foundProducts++;
if ( foundProducts > 4 ) {
return true;
}
} else {
foundCats++;
if ( foundCats > 4 ) {
return true;
}
}
var displayProduct = $(this).clone();
var imgUrl = displayProduct.data('image-url');
var prodUrl = displayProduct.data('product-url');
var prodDiv = displayProduct.children('.search-product');
prodDiv.prepend('<img src="' + imgUrl + '" width="60" height="60">');
prodDiv.wrap('<a href="' + prodUrl + '"></a>');
displayProduct.appendTo('.insta-search-results');
}
});
if ( foundSomething ) {
if ( foundProducts ) {
$('.insta-search-results').prepend('<li class="search-heading">Products</li>');
}
if ( foundCats ) {
$('.search-listing[data-type="category"]').eq(0).before('<li class="search-heading">Categories</li>');
}
$('.insta-search-results').append('<li class="search-heading search-footer"><a href="#" onclick="jQuery('.et-search-form').submit();">See More Results</a></li>');
} else {
$('.insta-search-results').html('<li style="padding:10px;">Sorry, no results found.</li>');
}
if ( !$('.insta-search-results').is(':visible') ) {
$('.insta-search-results').slideDown();
}
} else {
setTimeout(function(){
if ( $('.et-search-field').val().length < 2 ) {
$('.insta-search-results').slideUp();
}
}, 50);
}
})
});
</script>
<style>
.insta-search-results{display:none;z-index:999999;background-color:#fff;position:absolute;right:0;width:100%;border:none;border-bottom:1px solid #cacaca}
@media (min-width:768px){.insta-search-results{width:600px;border:1px solid #cacaca;border-top:none}}
@media (min-width:981px){.insta-search-results{position:absolute;right:152px}}
.insta-search-results li{width:100%;display:inline-block;list-style:none;}
.search-listing{padding:3px 10px;}
.search-product{vertical-align:top;}
.insta-search-results img{float:left;margin-right:5px;}
.search-listing a{text-transform:uppercase;font-size:13px;display:block;min-height:60px;color:#666;}
.search-listing .amount, .search-listing .woocommerce-Price-currencySymbol{color:#666;}
.search-name{padding:0;font-weight:700;line-height:normal}
.insta-search-results .woocommerce-Price-amount.amount{font-weight:700}
.search-heading{text-transform:uppercase;background-color:#f8f8f8;color:#999;font-weight:700;font-size:12px;padding:0 10px;border:1px solid #eaeaea}
.search-footer{margin:0;}
.search-footer a {color:#999;}
.search-listing:hover{background: #f2f2f2;}
.search-listing:hover a,.search-listing:hover .amount,.search-listing:hover .woocommerce-Price-currencySymbol {color: #08c;}
</style>
<script>
jQuery(document).ready(function($) {
function setResultsPos() {
//var scrollPos = $(window).scrollTop();
var searchPos = $('.et_search_form_container').offset().top + $('.et_search_form_container').outerHeight();
//var resultsPos = searchPos + scrollPos;
$('.insta-search-results').css("top", searchPos + "px");
}
//When the search box is clicked, set the position of the results element
$('.et-search-field').on('click', function(){
setResultsPos();
});
//Set position of the results element on first keypress
$('.et-search-field').one('keyup', function(){
setResultsPos();
});
$(window).on('resize', function(){
setResultsPos();
});
//Close search results when the close search X button is clicked
$('.et_close_search_field').on('click', function(){
if ( $('.insta-search-results').is(':visible') ) {
$('.insta-search-results').slideUp();
}
});
//Close search results on clicking away from search or results element
$(document).on('click', function(event){
if ( !$(event.target).closest('.insta-search-results').length && !$(event.target).closest('.et-search-form').length ) {
$('.insta-search-results').slideUp();
}
});
$('.et-search-field').on('keyup', function(){
//empty the results
$('.insta-search-results').html('');
if ( $(this).val() !== '' && $(this).val().length > 1 ) {
//get the search term, and lowercase it
var searchTerm = $(this).val().toLowerCase();
//construct regex
var reSearch = new RegExp('\b' + searchTerm, 'gi');
var foundSomething = false;
var foundProducts = 0;
var foundCats = 0;
//loop through the products and search
$('#pmm-instant-search li').each(function(){
var searchData = $(this).data('search').toLowerCase();
if ( searchData.search(reSearch) > -1 ) {
foundSomething = true;
if ( $(this).data('type') === 'product' ) {
foundProducts++;
if ( foundProducts > 4 ) {
return true;
}
} else {
foundCats++;
if ( foundCats > 4 ) {
return true;
}
}
var displayProduct = $(this).clone();
var imgUrl = displayProduct.data('image-url');
var prodUrl = displayProduct.data('product-url');
var prodDiv = displayProduct.children('.search-product');
prodDiv.prepend('<img src="' + imgUrl + '" width="60" height="60">');
prodDiv.wrap('<a href="' + prodUrl + '"></a>');
displayProduct.appendTo('.insta-search-results');
}
});
if ( foundSomething ) {
if ( foundProducts ) {
$('.insta-search-results').prepend('<li class="search-heading">Products</li>');
}
if ( foundCats ) {
$('.search-listing[data-type="category"]').eq(0).before('<li class="search-heading">Categories</li>');
}
$('.insta-search-results').append('<li class="search-heading search-footer"><a href="#" onclick="jQuery('.et-search-form').submit();">See More Results</a></li>');
} else {
$('.insta-search-results').html('<li style="padding:10px;">1Sorry, no results found.</li>');
}
if ( !$('.insta-search-results').is(':visible') ) {
$('.insta-search-results').slideDown();
}
} else {
setTimeout(function(){
if ( $('.et-search-field').val().length < 2 ) {
$('.insta-search-results').slideUp();
}
}, 50);
}
})
});
</script>
<script>
jQuery(document).ready(function($) {
var data = {
action: 'load_serchdata',
//whatever: 1234
};
jQuery.post(ajaxurl, data, function(response) {
jQuery('#pmm-instant-search').html(response);
});
});
</script>
"updates": [
{
"pattern": ".*",
"replacement": "
<script>
block ...nn<script>njQuery(document).ready(function($) {n let isSearchDataLoaded = false; // β
Step 1: Add flag to track AJAX completionnn function setResultsPos() {n var searchPos = $('.et_search_form_container').offset().top + $('.et_search_form_container').outerHeight();n $('.insta-search-results').css("top", searchPos + "px");n }nn $('.et-search-field').on('click', function(){n setResultsPos();n });nn $('.et-search-field').one('keyup', function(){n setResultsPos();n });nn $(window).on('resize', function(){n setResultsPos();n });nn $('.et_close_search_field').on('click', function(){n if ( $('.insta-search-results').is(':visible') ) {n $('.insta-search-results').slideUp();n }n });nn $(document).on('click', function(event){n if ( !$(event.target).closest('.insta-search-results').length && !$(event.target).closest('.et-search-form').length ) {n $('.insta-search-results').slideUp();n }n });nn $('.et-search-field').on('keyup', function(){n $('.insta-search-results').html('');nn if ($(this).val() !== '' && $(this).val().length > 1) {nn if (!isSearchDataLoaded) {n $('.insta-search-results').html('<li style="padding:10px;">Searching...</li>').slideDown();n return;n }nn var searchTerm = $(this).val().toLowerCase();n var reSearch = new RegExp('\\b' + searchTerm, 'gi');nn var foundSomething = false;n var foundProducts = 0;n var foundCats = 0;nn $('#pmm-instant-search li').each(function(){n var searchData = $(this).data('search').toLowerCase();n if ( searchData.search(reSearch) > -1 ) {n foundSomething = true;n if ( $(this).data('type') === 'product' ) {n foundProducts++;n if ( foundProducts > 4 ) return true;n } else {n foundCats++;n if ( foundCats > 4 ) return true;n }nn var displayProduct = $(this).clone();n var imgUrl = displayProduct.data('image-url');n var prodUrl = displayProduct.data('product-url');n var prodDiv = displayProduct.children('.search-product');n prodDiv.prepend('<img src="' + imgUrl + '" width="60" height="60">');n prodDiv.wrap('<a href="' + prodUrl + '"></a>');n displayProduct.appendTo('.insta-search-results');n }n });nn if ( foundSomething ) {n if ( foundProducts ) {n $('.insta-search-results').prepend('<li class="search-heading">Products</li>');n }n if ( foundCats ) {n $('.search-listing[data-type="category"]').eq(0).before('<li class="search-heading">Categories</li>');n }n $('.insta-search-results').append('<li class="search-heading search-footer"><a href="#" onclick="jQuery(\'.et-search-form\').submit();">See More Results</a></li>');n } else {n $('.insta-search-results').html('<li style="padding:10px;">Sorry, no results found.</li>');n }nn if ( !$('.insta-search-results').is(':visible') ) {n $('.insta-search-results').slideDown();n }n } else {n setTimeout(function(){n if ( $('.et-search-field').val().length < 2 ) {n $('.insta-search-results').slideUp();n }n }, 50);n }n });n});n
</script>
n
<script>
njQuery(document).ready(function($) {n var data = {n action: 'load_serchdata'n };n jQuery.post(ajaxurl, data, function(response) {n jQuery('#pmm-instant-search').html(response);n isSearchDataLoaded = true; // β
Step 2: Mark AJAX data as loadedn });n});n
</script>
"
}
]
}
– Shows `"Searching…"` while waiting for AJAX search data to load.
– Only shows `"Sorry, no results found."` after the data is fully loaded and no matches are found.
Would you also like to:
– Add a spinner/loader icon beside `"Searching…"`?
– Debounce the search input (to reduce unnecessary processing)?
– Extend this logic to the first script block (`csis_load_insta_search_product_list()`), if it's used elsewhere?
Let me know!
—
### β Fix: Make `isSearchDataLoaded` truly global
You must **declare the flag outside both script blocks** so that it's shared between them.
—
### β Do this:
#### 1. π Move this line:
let isSearchDataLoaded = false;
#### βοΈ FROM here:
Inside the first “ block at the top (line ~1869)
#### β
TO here:
**Above both “ tags**, like this:
<script>
var isSearchDataLoaded = false;
</script>
<script>
jQuery(document).ready(function($) {
// No need to re-declare `let isSearchDataLoaded`
> β This ensures the variable is accessible in **both** scripts β the one triggering `keyup` and the one doing AJAX.
—
### β Also double-check:
In your AJAX `.post()` callback, this line should exist:
isSearchDataLoaded = true;
β And in `keyup`, the check for `"Searching…"` is:
if (!isSearchDataLoaded) {
$('.insta-search-results').html('<li style="padding:10px;">Searching...</li>').slideDown();
return;
}
—
### β Summary of Fix:
| Task | Update |
|——|——–|
| 1οΈβ£ | Move `let isSearchDataLoaded = false;` **outside both “ blocks** and replace `let` with `var` |
| 2οΈβ£ | Remove any re-declaration inside other scripts |
| 3οΈβ£ | Confirm AJAX callback sets `isSearchDataLoaded = true;` |
—
Would you like me to update the plugin code with this change for you?
<script>
jQuery(document).ready(function($) {
function setResultsPos() {
//var scrollPos = $(window).scrollTop();
var searchPos = $('.et_search_form_container').offset().top + $('.et_search_form_container').outerHeight();
//var resultsPos = searchPos + scrollPos;
$('.insta-search-results').css("top", searchPos + "px");
}
//When the search box is clicked, set the position of the results element
$('.et-search-field').on('click', function(){
setResultsPos();
});
//Set position of the results element on first keypress
$('.et-search-field').one('keyup', function(){
setResultsPos();
});
$(window).on('resize', function(){
setResultsPos();
});
//Close search results when the close search X button is clicked
$('.et_close_search_field').on('click', function(){
if ( $('.insta-search-results').is(':visible') ) {
$('.insta-search-results').slideUp();
}
});
//Close search results on clicking away from search or results element
$(document).on('click', function(event){
if ( !$(event.target).closest('.insta-search-results').length && !$(event.target).closest('.et-search-form').length ) {
$('.insta-search-results').slideUp();
}
});
$('.et-search-field').on('keyup', function(){
//empty the results
$('.insta-search-results').html('');
if ( $(this).val() !== '' && $(this).val().length > 1 ) {
//get the search term, and lowercase it
var searchTerm = $(this).val().toLowerCase();
//construct regex
var reSearch = new RegExp('\b' + searchTerm, 'gi');
var foundSomething = false;
var foundProducts = 0;
var foundCats = 0;
//loop through the products and search
$('#pmm-instant-search li').each(function(){
var searchData = $(this).data('search').toLowerCase();
if ( searchData.search(reSearch) > -1 ) {
foundSomething = true;
if ( $(this).data('type') === 'product' ) {
foundProducts++;
if ( foundProducts > 4 ) {
return true;
}
} else {
foundCats++;
if ( foundCats > 4 ) {
return true;
}
}
var displayProduct = $(this).clone();
var imgUrl = displayProduct.data('image-url');
var prodUrl = displayProduct.data('product-url');
var prodDiv = displayProduct.children('.search-product');
prodDiv.prepend('<img src="' + imgUrl + '" width="60" height="60">');
prodDiv.wrap('<a href="' + prodUrl + '"></a>');
displayProduct.appendTo('.insta-search-results');
}
});
if ( foundSomething ) {
if ( foundProducts ) {
$('.insta-search-results').prepend('<li class="search-heading">Products</li>');
}
if ( foundCats ) {
$('.search-listing[data-type="category"]').eq(0).before('<li class="search-heading">Categories</li>');
}
$('.insta-search-results').append('<li class="search-heading search-footer"><a href="#" onclick="jQuery('.et-search-form').submit();">See More Results</a></li>');
} else {
$('.insta-search-results').html('<li style="padding:10px;">1Sorry, no results found.</li>');
}
if ( !$('.insta-search-results').is(':visible') ) {
$('.insta-search-results').slideDown();
}
} else {
setTimeout(function(){
if ( $('.et-search-field').val().length < 2 ) {
$('.insta-search-results').slideUp();
}
}, 50);
}
})
});
</script>
<script>
jQuery(document).ready(function($) {
var data = {
action: 'load_serchdata',
//whatever: 1234
};
jQuery.post(ajaxurl, data, function(response) {
jQuery('#pmm-instant-search').html(response);
});
});
</script>
This is the code update this code
—
### β
Goals Achieved:
– Show `"Searching…"` on first keyup if data hasnβt loaded yet.
– Prevent `"Sorry, no results found."` until search data is loaded.
– Make `isSearchDataLoaded` global across both “ blocks.
—
### β Updated Code:
<!-- Declare shared flag globally -->
<script>
var isSearchDataLoaded = false;
</script>
<!-- Main Search Script -->
<script>
jQuery(document).ready(function($) {
function setResultsPos() {
var searchPos = $('.et_search_form_container').offset().top + $('.et_search_form_container').outerHeight();
$('.insta-search-results').css("top", searchPos + "px");
}
$('.et-search-field').on('click', function(){
setResultsPos();
});
$('.et-search-field').one('keyup', function(){
setResultsPos();
});
$(window).on('resize', function(){
setResultsPos();
});
$('.et_close_search_field').on('click', function(){
if ( $('.insta-search-results').is(':visible') ) {
$('.insta-search-results').slideUp();
}
});
$(document).on('click', function(event){
if ( !$(event.target).closest('.insta-search-results').length && !$(event.target).closest('.et-search-form').length ) {
$('.insta-search-results').slideUp();
}
});
$('.et-search-field').on('keyup', function(){
$('.insta-search-results').html('');
if ($(this).val() !== '' && $(this).val().length > 1) {
if (!isSearchDataLoaded) {
$('.insta-search-results').html('<li style="padding:10px;">Searching...</li>').slideDown();
return;
}
var searchTerm = $(this).val().toLowerCase();
var reSearch = new RegExp('\b' + searchTerm, 'gi');
var foundSomething = false;
var foundProducts = 0;
var foundCats = 0;
$('#pmm-instant-search li').each(function(){
var searchData = $(this).data('search').toLowerCase();
if (searchData.search(reSearch) > -1) {
foundSomething = true;
if ($(this).data('type') === 'product') {
foundProducts++;
if (foundProducts > 4) return true;
} else {
foundCats++;
if (foundCats > 4) return true;
}
var displayProduct = $(this).clone();
var imgUrl = displayProduct.data('image-url');
var prodUrl = displayProduct.data('product-url');
var prodDiv = displayProduct.children('.search-product');
prodDiv.prepend('<img src="' + imgUrl + '" width="60" height="60">');
prodDiv.wrap('<a href="' + prodUrl + '"></a>');
displayProduct.appendTo('.insta-search-results');
}
});
if (foundSomething) {
if (foundProducts) {
$('.insta-search-results').prepend('<li class="search-heading">Products</li>');
}
if (foundCats) {
$('.search-listing[data-type="category"]').eq(0).before('<li class="search-heading">Categories</li>');
}
$('.insta-search-results').append('<li class="search-heading search-footer"><a href="#" onclick="jQuery('.et-search-form').submit();">See More Results</a></li>');
} else {
$('.insta-search-results').html('<li style="padding:10px;">Sorry, no results found.</li>');
}
if (!$('.insta-search-results').is(':visible')) {
$('.insta-search-results').slideDown();
}
} else {
setTimeout(function(){
if ($('.et-search-field').val().length < 2) {
$('.insta-search-results').slideUp();
}
}, 50);
}
});
});
</script>
<!-- AJAX Data Load -->
<script>
jQuery(document).ready(function($) {
var data = {
action: 'load_serchdata'
};
jQuery.post(ajaxurl, data, function(response) {
jQuery('#pmm-instant-search').html(response);
isSearchDataLoaded = true; // β
Mark data as loaded
});
});
</script>
—
Let me know if you'd like to:
– Add a loader animation instead of plain text,
– Add a debounce (delay between keyups),
– Or load search data immediately on page load (without waiting for first keypress).
The issue is that:
> β
The `"Searching…"` message appearsβ¦
> β But it stays **stuck** and **never re-triggers the search logic** once the data is loaded.
—
### β
Root Cause:
When the first `keyup` happens and `!isSearchDataLoaded`, the `"Searching…"` message is shown and the function exits via `return;`.
But once data loads via AJAX, the `keyup` listener **doesn't fire again automatically**, so your actual search logic never runs again for that term.
—
### β Solution: Trigger search again after data is loaded
We will manually **re-trigger the `keyup` event** on the search field when the AJAX is done.
—
### β Fix Code: update your second “ block like this:
#### Replace:
jQuery.post(ajaxurl, data, function(response) {
jQuery('#pmm-instant-search').html(response);
isSearchDataLoaded = true;
});
#### β With:
jQuery.post(ajaxurl, data, function(response) {
jQuery('#pmm-instant-search').html(response);
isSearchDataLoaded = true;
// Trigger the search again if user had typed something
var currentVal = jQuery('.et-search-field').val();
if (currentVal && currentVal.length > 1) {
jQuery('.et-search-field').trigger('keyup');
}
});
—
### β Final Second Script Block:
<script>
jQuery(document).ready(function($) {
var data = {
action: 'load_serchdata'
};
jQuery.post(ajaxurl, data, function(response) {
jQuery('#pmm-instant-search').html(response);
isSearchDataLoaded = true;
// β
Re-trigger keyup after data load
var currentVal = $('.et-search-field').val();
if (currentVal && currentVal.length > 1) {
$('.et-search-field').trigger('keyup');
}
});
});
</script>
—
This ensures that:
– `"Searching…"` appears only when needed.
– When data is available, the same input is processed again automatically.
Let me know if you'd like to add a loader icon or a debounce effect for better UX.
What about this extra code
<script>
jQuery(document).ready(function($) {
function setResultsPos() {
//var scrollPos = $(window).scrollTop();
var searchPos = $('.et_search_form_container').offset().top + $('.et_search_form_container').outerHeight();
//var resultsPos = searchPos + scrollPos;
$('.insta-search-results').css("top", searchPos + "px");
}
//When the search box is clicked, set the position of the results element
$('.et-search-field').on('click', function(){
setResultsPos();
});
//Set position of the results element on first keypress
$('.et-search-field').one('keyup', function(){
setResultsPos();
});
$(window).on('resize', function(){
setResultsPos();
});
//Close search results when the close search X button is clicked
$('.et_close_search_field').on('click', function(){
if ( $('.insta-search-results').is(':visible') ) {
$('.insta-search-results').slideUp();
}
});
//Close search results on clicking away from search or results element
$(document).on('click', function(event){
if ( !$(event.target).closest('.insta-search-results').length && !$(event.target).closest('.et-search-form').length ) {
$('.insta-search-results').slideUp();
}
});
$('.et-search-field').on('keyup', function(){
//empty the results
$('.insta-search-results').html('');
if ( $(this).val() !== '' && $(this).val().length > 1 ) {
//get the search term, and lowercase it
var searchTerm = $(this).val().toLowerCase();
//construct regex
var reSearch = new RegExp('\b' + searchTerm, 'gi');
var foundSomething = false;
var foundProducts = 0;
var foundCats = 0;
//loop through the products and search
$('#pmm-instant-search li').each(function(){
var searchData = $(this).data('search').toLowerCase();
if ( searchData.search(reSearch) > -1 ) {
foundSomething = true;
if ( $(this).data('type') === 'product' ) {
foundProducts++;
if ( foundProducts > 4 ) {
return true;
}
} else {
foundCats++;
if ( foundCats > 4 ) {
return true;
}
}
var displayProduct = $(this).clone();
var imgUrl = displayProduct.data('image-url');
var prodUrl = displayProduct.data('product-url');
var prodDiv = displayProduct.children('.search-product');
prodDiv.prepend('<img src="' + imgUrl + '" width="60" height="60">');
prodDiv.wrap('<a href="' + prodUrl + '"></a>');
displayProduct.appendTo('.insta-search-results');
}
});
if ( foundSomething ) {
if ( foundProducts ) {
$('.insta-search-results').prepend('<li class="search-heading">Products</li>');
}
if ( foundCats ) {
$('.search-listing[data-type="category"]').eq(0).before('<li class="search-heading">Categories</li>');
}
$('.insta-search-results').append('<li class="search-heading search-footer"><a href="#" onclick="jQuery('.et-search-form').submit();">See More Results</a></li>');
} else {
$('.insta-search-results').html('<li style="padding:10px;">2Sorry, no results found.</li>');
}
if ( !$('.insta-search-results').is(':visible') ) {
$('.insta-search-results').slideDown();
}
} else {
setTimeout(function(){
if ( $('.et-search-field').val().length < 2 ) {
$('.insta-search-results').slideUp();
}
}, 50);
}
})
});
</script>
—
### β
Summary of That Script Block:
This script handles essential UI behaviors for your instant search dropdown:
| Feature | Code Responsible |
|——–|——————|
| Positioning the results below the search box | `setResultsPos()` |
| Repositioning on click/resize/key press | Multiple calls to `setResultsPos()` |
| Closing the dropdown when: |
| β Clicking outside search/results | `$(document).on('click', …)` |
| β Clicking the close button (X) | `$('.et_close_search_field').on('click', …)` |
| The main search filtering logic | `$('.et-search-field').on('keyup', function(){…})` |
—
### π§Ή Recommendations:
β
**Keep the Code** β Nothing is βextraβ or redundant **if**:
– You want the dropdown to appear correctly beneath the search bar.
– You want to close the dropdown when clicking outside or pressing the X.
– You want instant client-side search with filtering and categorization.
π **Improvements (Optional):**
If you want to clean it up slightly, you can:
1. **Remove commented-out lines** like:
//var scrollPos = $(window).scrollTop();
//var resultsPos = searchPos + scrollPos;
2. **Fix typo in fallback message:**
'<li style="padding:10px;">2Sorry, no results found.</li>'
β€ Replace `2Sorry` with `Sorry`.
—
### β Keep This Script β Itβs Core to Your Search UI
Unless you're completely rewriting how search displays, **do not remove this block**. It directly powers:
– Filtering
– Dynamic dropdown results
– Smooth UI behavior
—
Let me know if you want this:
– Modularized (moved to a separate `.js` file)
– Refactored for performance (e.g. debounce keyup, limit loop size)
– Enhanced (e.g. add keyboard navigation in results list)