0

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.

2
  • Is this not working or what is the issue here?
    – tier1
    Apr 7, 2015 at 20:19
  • How does it fail? Never changes? Actually throws an exception?
    – ryanyuyu
    Apr 7, 2015 at 20:32

2 Answers 2

4

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" />

1
  • Yee. Thank you very much!
    – Newester
    Apr 7, 2015 at 20:26
0

Have you tried :

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.