Search code examples
javascriptjquerytwitter-bootstrapsearch-box

Attach div to a search textbox to create Gmail like search box


I am creating a Gmail like search box where I click on the text box and it opens a new div where I can enter search criteria for various fields. How can I tightly attach the TextBox('#searchTextBox) or the div containing the textbox with the div for search form ('#gmailSearchBox)? Also I am using bootstrap responsive and thus the location and size of the text box changes. So just setting div style display:block and display:none doesnt work. Thanks.

Html Code:

<div style="border:1px solid grey; border-radius:3px; max-width:300px; padding-right:0px; padding-left:0px;"class="container-fluid">
    <div style="left:0px;right:20px; line-height:inherit; float:left; width:100%; ">
        <input id="searchTextBox" type="text" style="border:none; width:95%; line-height:inherit; padding:0px 0px 0px 0px;"> 
        <a id="showSearchBox" href="#" style="height:20px">
             <span class="caret" style="vertical-align:middle; height:100%"> </span>
        </a>
    </input>
</div>
</div>
<input type="button" value="Search" Class="btn btn-primary" /><br>
Various<br>
Other<br>
Information<br>
Goes <br>
Here<br>
<div id='gmailSearchBox' class="gmailSearchBox" style="position:absolute; display:none; border:1px solid grey">
<form action="" method="post">
    From:<input type="text" id="fromDate">
    <br/>
    To: <input type="text" id="toDate">
    <br/>
    Type:<select id="logType">
            <option>--Select Type--</option>
        </select>
    <br/>
    Text:<input type="text" id="searchText">
    <br/>
    <input type="submit" class="btn btn-primary">
</form>
</div>

Javascript Code

$('#showSearchBox').click(showSearchBox);
function showSearchBox() {
    $('#gmailSearchBox').css('display','block')
}

JsFiddle link - http://jsfiddle.net/7FBax/


Solution

  • If you want to do this with css, you'll probably need to change your markup a little. See the following fiddle, which does what you want: http://jsfiddle.net/97BwC/1/

    The revised Markup:

    <div id="search" style=""class="container-fluid">
    <div id="searchInputWrapper" style="">
        <input type="text" style=""> <a id="showSearchBox" href="#" style="height:20px">
                 <span class="caret" style="vertical-align:middle; height:100%"> </span>
            </a>
    
        </input>
    </div>
    <div id='gmailSearchBox' class="gmailSearchBox" style="">
        <form action="" method="post">From:
            <input type="text" id="fromDate">
            <br/>To:
            <input type="text" id="toDate">
            <br/>Type:
            <select id="logType">
                <option>--Select Type--</option>
            </select>
            <br/>Text:
            <input type="text" id="searchText">
            <br/>
            <input type="submit" class="btn btn-primary">
        </form>
    </div>
    

    The css:

        #search {
            position:relative;
            border:1px solid grey;
            border-radius:3px;
            max-width:300px;
            padding-right:0px;
            padding-left:0px;
        }
        #searchInputWrapper {
            left:0px;
            right:20px;
            line-height:inherit;
            float:left;
            width:100%;
        }
        #searchInputWrapper input {
            border:none;
            width:95%;
            line-height:inherit;
            padding:0px 0px 0px 0px;
        }
        #gmailSearchBox {
            position:absolute;
            bottom:-212px;
            right:0;
            display:none;
            border:1px solid grey;
            background-color:#fff;
        }
    

    Update:

    If the height of the search box isn't static, you can use javascript to set the bottom offset. Here's an updated fiddle: http://jsfiddle.net/GPTJ4/2/

    And here's the revised handler:

    $('#showSearchBox').click(showSearchBox);
    
    function showSearchBox() {
        var height = 0,
            $searchBox = $('#gmailSearchBox');
    
        $searchBox.css({display:'block'});
    
        //get the height of the search box
        height = $searchBox.height();
    
        //set the offset equal to the height
        $searchBox.css({bottom:-height +'px'});
    }