The live search disappears when you you click anywhere on the screen

Solved

The live search disappears when you you click anywhere on the screen

birinoc
Visitor
2 0 1

Hey! Having a little issue and would love if someone helps me solve it. Basically my website has live search and upon search the product appears but if anywhere on the white space I click the search disappears and upon reclicking on the search bar nothing comes up until you write enter a letter or delete a letter as search begins again. Can someone help me you solve it? I want that upon clicking on the rest area of the search the suggested search options are still there and also the product page of the clicked item is only opened when either the name or the image of the product is clicked, I want that the whole horizontal rectangular area of the specific product when clicked opens the specific product page. Thanks! Attaching items below, Website link: https://w0v9jv-y1.myshopify.com   Pass: door

 

birinoc_0-1731755649249.png

Search appears ⬆️

 

birinoc_1-1731755700231.png

Supposingly If I click in the area circled area the search disappears, I want it to stay as it is and also 

 

birinoc_2-1731755788404.png

If I click on the green area the product page of the item opens but If I click anywhere horizontal to the product page in the red area nothing happens, I want that even if the red area is clicked the search works and product page is opened!

Accepted Solution (1)

akshay_bhatt
Shopify Partner
115 10 13

This is an accepted solution.

Hi @birinoc ,

Here’s how you can fix both issues with your live search functionality:


1. Prevent the Search Dropdown from Disappearing on Background Click:

The issue occurs because the dropdown likely closes when a click event is detected outside the search area. To resolve this:

Steps:

  • Locate the JavaScript file responsible for the search dropdown functionality in your theme files. It might be named something like search.js or embedded within theme.js .
  • Find the code handling the "click outside" behavior. Look for a listener like document.addEventListner('click', ...).
  • Modify the code to ignore clicks inside the search area.

Example Fix:

document.addEventListener('click', function (e) {
  const searchBox = document.querySelector('.search-dropdown'); // Adjust selector as needed
  const searchInput = document.querySelector('.search-input'); // Adjust selector as needed

  if (!searchBox.contains(e.target) && !searchInput.contains(e.target)) {
    // Hide dropdown only if click is outside
    searchBox.classList.remove('visible');
  } else {
    // Keep dropdown visible if clicked inside
    searchBox.classList.add('visible');
  }
});

 

2. Make the Entire Product Row Clickable:

The issue arises because only specific elements (like product name or image) are linked to the product page. To make the entire row clickable:

Steps:

  • Go to the search dropdown's HTML template, likely in search.liquid or search-results.liquid .
  • Wrap the entire product row in an anchor tag ( <a>), or add a click event to the parent container.

Example HTML Update:

<div class="search-result-item">
  <a href="{{ product.url }}" class="search-result-link">
    <div class="search-result-content">
      <img src="{{ product.image | img_url: 'small' }}" alt="{{ product.title }}">
      <span>{{ product.title }}</span>
    </div>
  </a>
</div>

 

Example CSS Update:

Ensure the clickable area spans the entire row:

.search-result-link {
  display: flex;
  align-items: center;
  padding: 10px;
  text-decoration: none;
  width: 100%;
}

.search-result-link:hover {
  background-color: #f8f8f8; /* Optional hover effect */
}

 

3. Testing:

  • Save your changes and refresh your site.
  • Verify:
    1. The search dropdown remains visible when clicking inside.
    2. Clicking anywhere within a product’s row opens its page.

 

 

     

Got it! If you found my suggestions helpful, please consider liking or marking it as a solution.
Your feedback is appreciated! If you have any more questions or need further assistance, just let me know.

Thanks & Regards
Akshay Bhatt

- Need a Shopify Specialist?
- Custom Design | Advanced Coding | Store Modifications
Email us

View solution in original post

Replies 2 (2)

akshay_bhatt
Shopify Partner
115 10 13

This is an accepted solution.

Hi @birinoc ,

Here’s how you can fix both issues with your live search functionality:


1. Prevent the Search Dropdown from Disappearing on Background Click:

The issue occurs because the dropdown likely closes when a click event is detected outside the search area. To resolve this:

Steps:

  • Locate the JavaScript file responsible for the search dropdown functionality in your theme files. It might be named something like search.js or embedded within theme.js .
  • Find the code handling the "click outside" behavior. Look for a listener like document.addEventListner('click', ...).
  • Modify the code to ignore clicks inside the search area.

Example Fix:

document.addEventListener('click', function (e) {
  const searchBox = document.querySelector('.search-dropdown'); // Adjust selector as needed
  const searchInput = document.querySelector('.search-input'); // Adjust selector as needed

  if (!searchBox.contains(e.target) && !searchInput.contains(e.target)) {
    // Hide dropdown only if click is outside
    searchBox.classList.remove('visible');
  } else {
    // Keep dropdown visible if clicked inside
    searchBox.classList.add('visible');
  }
});

 

2. Make the Entire Product Row Clickable:

The issue arises because only specific elements (like product name or image) are linked to the product page. To make the entire row clickable:

Steps:

  • Go to the search dropdown's HTML template, likely in search.liquid or search-results.liquid .
  • Wrap the entire product row in an anchor tag ( <a>), or add a click event to the parent container.

Example HTML Update:

<div class="search-result-item">
  <a href="{{ product.url }}" class="search-result-link">
    <div class="search-result-content">
      <img src="{{ product.image | img_url: 'small' }}" alt="{{ product.title }}">
      <span>{{ product.title }}</span>
    </div>
  </a>
</div>

 

Example CSS Update:

Ensure the clickable area spans the entire row:

.search-result-link {
  display: flex;
  align-items: center;
  padding: 10px;
  text-decoration: none;
  width: 100%;
}

.search-result-link:hover {
  background-color: #f8f8f8; /* Optional hover effect */
}

 

3. Testing:

  • Save your changes and refresh your site.
  • Verify:
    1. The search dropdown remains visible when clicking inside.
    2. Clicking anywhere within a product’s row opens its page.

 

 

     

Got it! If you found my suggestions helpful, please consider liking or marking it as a solution.
Your feedback is appreciated! If you have any more questions or need further assistance, just let me know.

Thanks & Regards
Akshay Bhatt

- Need a Shopify Specialist?
- Custom Design | Advanced Coding | Store Modifications
Email us
birinoc
Visitor
2 0 1

Thank you Akshay! The Entire Row clickable solution worked!, but I still am not able to implement the product dropdown disappering on click. Could you help me fix it if I could add you as a staff? Thanks!