Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trigger a function with jQuery after a single letter was typed in an input box

I'm trying to make a simple search box. How can I trigger a function just after a single letter was typed in an input box? I used .keyup, but this seems to be useless, because when a diacritic character is typed, the function is triggered three times.

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

The above code works fine unless you type any diacritic character: ą, ś, ł, ż, ź, ć, etc.

I can't use .keypress or .keydown since they trigger a function before a character is typed.

EDITED LATER:

I know that my example might 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 a type a diacritic character or a capital letter, the query is executed three or two times, which slows down the whole script. It won't make a big difference if you execute the script in a computer web browser, but there is significant lag in mobile browsers.

like image 772
lukeshek Avatar asked Dec 27 '22 02:12

lukeshek


1 Answers

I considered checking the value of the string compared to the last time it was changed. So keypresses don't run the function unless it causes the string to be changed.

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

like image 171
Tina CG Hoehr Avatar answered May 12 '23 13:05

Tina CG Hoehr