Running a function using jQuery after entering a single letter in the input field

I am trying to create a simple search string. How can I call a function immediately after entering one letter in the input field? I used .keyup, but this seems useless because when a diacritical character is typed, the function runs three times.

<input id="text" type="text" />
<div></div>
<script>
    $('#text').keyup(function(){
        $('div').append('TEST'+$(this).val());
    });
</script>

The code above works fine if you do not type any diacritical character: ą, ś, ł, ż, ź, ć, etc.

I can not use .keypress or .keydown, because they run the function before entering the character.

EDIT LATER:

I know that my example may not be clear enough to understand the problem, but ... In my real code, every time a letter is typed, an SQLite query is executed. If the type is diacritical or uppercase, the query is executed three or two times, which slows down the entire script. It will not make much difference if you run the script in the computer’s web browser, but there is a significant lag in mobile browsers.

+3
source share
2 answers

I was considering checking the value of a string compared to a previous change. Thus, keystrokes do not start a function unless it causes a line change.

http://jsfiddle.net/82sTJ/1/

+5
source
var i = 0;    

$('#text').keyup(function(event) {
    i++;
    if(i == 1)
    {
        $('div').append('TEST'+$(this).val());
    }
});

This should work for me, you can also detect the entered specific key

+4

All Articles