How to make the event 'focusout' of an input?

0

I want to make an input appear when I click on the label , when I click outside the input, that is, focus on it, I would like it to disappear.

HTML:

<form action="/" class="search">
    <fieldset>
        <label for="pesquisar">
            <span>Buscar</span>
        </label>
        <input type="text" name="pesquisar">
        <button type="submit" class="send-search"></button>
    </fieldset>
</form>

JavaScript:

$(function() {
    var label = $('.search label'),
        input = $('.search label + input');

    label.on('click', function() {
        if (input.is(':focus')) {
            input.css('display', 'none');
        } else {
            input.css({
                'display': 'block'
            }).focus();
        }
    });

}(jQuery));

What am I doing wrong?

    
asked by anonymous 21.01.2016 / 16:25

4 answers

4

Hello,

HTML:

<form action="/" class="search">
<fieldset>
    <label for="pesquisar" id="meuLabel">
        <span>Buscar</span>
    </label>
    <input type="text" name="s" id="meuInput">
    <button type="submit" class="send-search">OK</button>
</fieldset>

Javascritpt:

$(function(){

var label = $('#meuLabel'),
    input = $('#meuInput');

input.hide();

label.on('click', function(){
    input.show();
});

input.on('focusout', function(){
    $(this).hide();
})

});

See the Fiddle

See the Fiddle with the answer

But you might consider using AngularJS would be a lot easier for you:

Google AngularJS

    
21.01.2016 / 16:41
1

So when you click on the label, go to the input, need to name the id, like the for example,

<label for="pesquisar">
        Buscar
    </label>
    <input type="text" id="pesquisar">

Example: link

In the matter of disappearing, do you want the input to disappear as long as the user clicks out of the input?

EDIT

To meet what you'd like, you could do like this:

$("#pesquisar").focusout(function(){
if($(this).val() == ""){
$(this).hide("slow", function(){
    $("label[for='pesquisar']").click(function(){
    $("#pesquisar").show("slow");
  })
});
}
})

See here: link

    
21.01.2016 / 16:27
1

You can do it this way ...

<label for="pesquisar">Buscar</label>
<input type="text" id="pesquisar">

and so javascript ...

$(document).ready(function(){
    $('label').click(function(e) {
      e.stopPropagation();
      if (!$('#pesquisar').is(':visible')) {
        $('#pesquisar').fadeIn('fast');
      } 
    });
    $('html').click(function() {
      if ($('#pesquisar').is(':visible')) {
        $('#pesquisar').fadeOut('fast');
      } 
    });
    $('#pesquisar').click(function(e){
      e.stopPropagation();
    });
});

You can see the example here ... link

    
21.01.2016 / 16:59
0

Like this?

var pesquisar = $("#pesquisar");
pesquisar.hide();
$("label[for='pesquisar']").click(function() {
  if (pesquisar.val().length <= 0) {
    pesquisar.toggle();
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script><labelfor="pesquisar">Buscar</label>
<input type="text" id="pesquisar">

I believe that toggle to show and hide the input when clicking on search would be more viable, because if you put the event to hide the input when clicking anywhere on the page it will often unnecessarily leave.

    
21.01.2016 / 16:34