All Collections
Advanced Design
Keep The Current Search Term in The Search Bar
Keep The Current Search Term in The Search Bar
Marc McCall avatar
Written by Marc McCall
Updated over a week ago

When a user is viewing search results, they might want to search again by modifying their search term (EX:. If they've made a typo).
โ€‹
It would be easier for the user if the current search term is still in the search bar and can be edited or removed entirely if they're going to search for something different, rather than typing out the search again.

Add the script below to your Footer Block in Theme Options

<script>

if ((window.location.search.indexOf('?query=') >= 0 || window.location.search.indexOf('?querybrand=') >= 0) ) {

// Get search text

const searchText = function searchText() {

let search = window.location.search.substring(7).split('&').shift(),

regex1 = /[+]+/gm;

return search.replace(regex1, " ");

};

var searchString = $('#breadcrumb span').html();

searchString = searchString.replace(/\s\s+/g, ' ');

var searchQuery = searchString.substr(searchString.indexOf(" ") + 15);

searchQuery = searchQuery.replace(/"|'/g, '');

console.log(searchQuery);

// Add the search results to search bar

const searchbar = document.querySelector('.header-full .widgetsearch input[type="text"]');

searchbar.value = searchQuery;

};

</script>

Did this answer your question?