Reputation: 1487
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
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