Renan Rodrigues
Renan Rodrigues

Reputation: 306

Hide and open div under certain conditions

I'm having trouble opening and closing a particular search bar.

I need to open a div while focus is in the field and take it out while it does not, but when you click on the div that is open the focus of the field can not be removed or closed.

I tried putting the event in the body, in a higher div however it did not work it continues to close.

Here is the code I have used:

$("input[name='search-input']").blur( function() {
     $(".divCompleteSearch").addClass('hiddendiv');
});

$("input[name='search-input']").focus( function() {
    $(".divCompleteSearch").removeClass('hiddendiv');
});

$(".button-search").on('click', function (e) {
    e.preventDefault();
    $("input[name='search-input']").focus();
    $(".active").removeClass("active");
    $(this).addClass('active');
});

HTML:

    <div class="row align-content-center">
    <div class="col search-bar">
        <div class="col m1 icon-search-bar">
            <svg viewBox="0 0 16 16" role="presentation" aria-hidden="true" focusable="false"
                 style="height: 22px; width: 22px; display: block; fill: currentcolor;">
                <path
                    d="m2.5 7c0-2.5 2-4.5 4.5-4.5s4.5 2 4.5 4.5-2 4.5-4.5 4.5-4.5-2-4.5-4.5m13.1 6.9-2.8-2.9c.7-1.1 1.2-2.5 1.2-4 0-3.9-3.1-7-7-7s-7 3.1-7 7 3.1 7 7 7c1.5 0 2.9-.5 4-1.2l2.9 2.8c.2.3.5.4.9.4.3 0 .6-.1.8-.4.5-.5.5-1.2 0-1.7"></path>
            </svg>
        </div>
        <div class="col m11 input-search-bar">
            <input type="text" name="search-input" placeholder="Experimente Colegio São Fransisco">
        </div>
    </div>
</div>
<div class="row align-content-center divCompleteSearch hiddendiv">
    <div class="col complete-search">
        <div class="row">
            <div class="col m3 button-search active">
                Escolas
            </div>
            <div class="col m3 button-search">
                Bairro
            </div>
            <div class="col m3 button-search">
                Telefone
            </div>
            <div class="col m3 button-search">
                Apelido
            </div>
        </div>
    </div>
</div>

inserir a descrição da imagem aqui

In this first image is the div closed, when there is the focus in theinput search it should open the second image, the div should remain open until it clicks anywhere that is not inside thediv open.

inserir a descrição da imagem aqui

Upvotes: 0

Views: 45

Answers (1)

VVV
VVV

Reputation: 7593

You need to remove the blur event and listen to any clicks on the page.

If you look at this fiddle, notice that I wrapped all the HTML in a container named #search-container. Then in the Javascript, I removed the blur() method and added a mouseup() listener on the whole document.

The mouseup() event checks if the user clicked on the container or one of it's children. If not, it closes the div.

$(document).mouseup(function(e) 
{
    var container = $("#search-container");

    // if the target of the click isn't the container nor a descendant of the container
    if (!container.is(e.target) && container.has(e.target).length === 0) 
    {
        $(".divCompleteSearch").addClass('hiddendiv');
    }
});

$("input[name='search-input']").focus( function() {
    $(".divCompleteSearch").removeClass('hiddendiv');
});

$(".button-search").on('click', function (e) {
    e.preventDefault();
    $("input[name='search-input']").focus();
    $(".active").removeClass("active");
    $(this).addClass('active');
});
.hiddendiv { display: none; }

#search-container {
  border:1px solid #ff0000;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.js"></script>
<div id="search-container">
<div class="row align-content-center">
   <div class="col search-bar">
      <div class="col m1 icon-search-bar">
         <svg viewBox="0 0 16 16" role="presentation" aria-hidden="true" focusable="false"
            style="height: 22px; width: 22px; display: block; fill: currentcolor;">
            <path
               d="m2.5 7c0-2.5 2-4.5 4.5-4.5s4.5 2 4.5 4.5-2 4.5-4.5 4.5-4.5-2-4.5-4.5m13.1 6.9-2.8-2.9c.7-1.1 1.2-2.5 1.2-4 0-3.9-3.1-7-7-7s-7 3.1-7 7 3.1 7 7 7c1.5 0 2.9-.5 4-1.2l2.9 2.8c.2.3.5.4.9.4.3 0 .6-.1.8-.4.5-.5.5-1.2 0-1.7"></path>
         </svg>
      </div>
      <div class="col m11 input-search-bar">
         <input type="text" name="search-input" placeholder="Experimente Colegio São Fransisco">
      </div>
   </div>
</div>
<div class="row align-content-center divCompleteSearch hiddendiv">
   <div class="col complete-search">
      <div class="row">
         <div class="col m3 button-search active">
            Escolas
         </div>
         <div class="col m3 button-search">
            Bairro
         </div>
         <div class="col m3 button-search">
            Telefone
         </div>
         <div class="col m3 button-search">
            Apelido
         </div>
      </div>
   </div>
</div>

Upvotes: 2

Related Questions