Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8990223
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T22:27:18+00:00 2026-06-15T22:27:18+00:00

I have a search box which, in it’s first start is a magnifying glass

  • 0

I have a search box which, in it’s first start is a magnifying glass icon. When clicked (:focus) it expands into a search box where you can type.

A friend of mine helped me with out by writing a jQuery code that when the user starts typing in the search field, results show instantly beneath it (from my mySQL database), which you can click to take you to the result.

However, if you click off the search field, and the CSS is no longer set to :focus the jQuery (lets call them) quick results, do not disappear, obviously – as nothing tells them too.

My question is – How can I make them show when the search box is :focus on and disappear when it is not.

This is the CSS for the search bar:

.search_bar input[type="text"] {
position: absolute;
right: 0px;
top: -18px;
    padding: 0 0 0 22px;
    width: 30px;
    height: 54px;
    -webkit-transition: width 0.5s ease-in-out;
}

.search_bar input[type="text"]:focus{
    width: 550px;
    outline: none;
}

And this is the CSS for the jQuery results (the space where they generate:

#searchResult {
position: absolute;
z-index: 1;
width: 536px;
right: 20px;
top: 40px;  
text-align: left;
}

This the the jQuery:

jQuery(document).ready(function()
    {
        jQuery('#search_bar').keyup(db_search);
    });

    function db_search()
    {
        var searchFor = jQuery('#search_bar').val();
        if (searchFor=="")
        {
            document.getElementById("searchResult").innerHTML="";
            return;
        } 
        if (window.XMLHttpRequest)
        {// code for IE7+, Firefox, Chrome, Opera, Safari
            xmlhttp=new XMLHttpRequest();
        }
        else
        {// code for IE6, IE5
            xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
        }
        xmlhttp.onreadystatechange=function()
        {
            if (xmlhttp.readyState==4 && xmlhttp.status==200)
            {
                document.getElementById("searchResult").innerHTML=xmlhttp.responseText;
            }
        }

        xmlhttp.open("GET","php/searchFor.php?search="+searchFor,true);
        xmlhttp.send();
    }

Here is some stills to illustrate my problem, if it is not clear.

This is the un focused search
This is when <code>:focus</code> is on
This is the jQuery 'Quick Search' links showing (as they should do)
This is my problem, when the search box slides back, the results stay, i want to hide them

I tried to use this CSS to hide it:

.search_bar input[type="text"]:focus + #searchResult {
display: block;
}

To display: block when :focus on search bar.

#searchResult {
position: absolute;
z-index: 1;
width: 536px;
right: 20px;
top: 40px;  
text-align: left;
display: none;
}

To hide the whole div and its content when it’s not :focus on the search bar.

But this doesn’t work and I can’t figure out how else I would do it, I still have a very very limited knowledge of jQuery/javascript so if the answer lies in this, could some one be kind enough to share the answer? I tried to research the hide() function in jQuery but I didn’t know where to place it, I assume it would be in an if statement?

Sorry for a long question, I tried to be as clear and thorough as possible.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T22:27:19+00:00Added an answer on June 15, 2026 at 10:27 pm

    You’ll probably want to take a look at the jquery .blur() event: http://api.jquery.com/blur/.

    You can try this (untested) code:

     jQuery('#search_bar')
         .keyup(db_search)
         .blur(function() { $("#searchResult").hide(); });
    

    Explanation: the blur event is called whenever the element loses focus. In this scenario, the user puts the focus on the #search_bar. When the #search_bar has a keyup event, it does the search as you were doing before, but when #search_bar loses focus (the user clicked somewhere else, or on one of the results) the #searchResults are hidden.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following problem: I have a search box in which user can
I have an edit text which functions as a search box in my application.
I have a master page that contains a search box which is validated as
i have a Ajax Control toolkit water mark extender for a search box which
In a big div I have search box which basically is a div having
I have a search box which has an auto-complete feature that I've been building.
I have a search box which performs a search on any given keyword. It
I have a search box with two tabs to choose which type to search.
I have an text input box which needs to be used as a search.
Ok, so I have this search box in which people typein a food item.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.