Newester
Newester

Reputation: 1487

jQuery - hide div when input length is less than 3 chafacters

I would like to hide a div when the input length is less than 3 characters long.

Here is my code:

<div id="Container" class="content">
<input type="text" name="search" class="search" id="searchid" placeholder="Wyszukaj..." /><br /> 
<div id="result"></div>
</div>

<script>
$('input[name=search]').change(function()
{
      if( $(this).val().length < 3 ) {
           jQuery("#result").fadeOut(); 
      }
});
</script>

IMPORTANT: That should work when user is changing this input.

Thanks.

Upvotes: 0

Views: 1627

Answers (2)

wahwahwah
wahwahwah

Reputation: 3177

You want to use the .keyup() event to capture while the user is changing the input value. change() will only fire when the input loses focus.

$('input[name="search"]').keyup(function() {
  if ($(this).val().length < 3) {
    $(this).fadeOut();
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>


<input name="search" placeholder="Hello" />

Upvotes: 4

AshBringer
AshBringer

Reputation: 2673

Have you tried :

$('#result').hide();

Upvotes: 0

Related Questions