The length <= 2 check is working ONLY if I’m manually erasing the text from the input.
However, My input has closing button which has reset(); on it, and when I’m pressing it, my input is empty, however jquery code doesnt care about that and doesnt recognise the input as empty, because as I know the reset(); function turns my input form to undefined.. so I have to check the input for undefined in my jQuery, however it doesn’t work. any clues??
<script>
$('.regulator').keyup(function() {
if ($(this).val().length <= 2 || $(this).val().value == undefined) {
$('.quickSearchResults').hide();
} else {
$('.quickSearchResults').show();
}
}).keyup();
</script>
2
Answers
I suggest you use this instead:
There is really no reason to check for undefined if you are using the value of the input the user is typing in.
.val()
is not going to return undefined when the user is interacting with it.Now if you really want to check for undefined, do that check first
how most people would code this
Now to handle reset, listen to the event.
or