Here is a simple way to implement an 'autocomplete' text field:
Define a function to use as a typewatch timer:
var typewatch = (function(){
var timer = 0;
return function(callback, ms){
clearTimeout (timer);
timer = setTimeout(callback, ms);
}
})();
Then use the timer in a .keyup() jQuery event listener.
$('#text-field').keyup(function(e) {
typewatch(function () {
tmpLen = $('#text-field').val().length;
if( tmpLen > 2 || tmpLen == 0 ) { // also searches when the field is cleared with backspace
getAutoComplete(); // or any other action
}
}, 500);
});
The listener will trigger a call to getAutoComplete() when the length of the text in the field is greater than 2 characters, or 0 characters for when the field is cleared.
Adjusting the > 2 characters or 500ms timeout will fine tune the behavior.
Snippets of code and tricks that would otherwise be forgotten. Useful for me, hopefully for you too.
Wednesday, March 27, 2013
Monday, March 25, 2013
Installing Cygwin
Download the installer from CNET:
http://download.cnet.com/Cygwin/3000-2212_4-10026629.html
Follow these instructions, they're very good:
http://www.mcclean-cooper.com/valentino/cygwin_install/
The X-windows part at the end is optional.
Then learn some fun stuff that's now possible, like grep and wget:
http://www.weekeat.com/post/24651647092/grep-basics-in-25-seconds
http://www.thegeekstuff.com/2009/09/the-ultimate-wget-download-guide-with-15-awesome-examples/
http://download.cnet.com/Cygwin/3000-2212_4-10026629.html
Follow these instructions, they're very good:
http://www.mcclean-cooper.com/valentino/cygwin_install/
The X-windows part at the end is optional.
Then learn some fun stuff that's now possible, like grep and wget:
http://www.weekeat.com/post/24651647092/grep-basics-in-25-seconds
http://www.thegeekstuff.com/2009/09/the-ultimate-wget-download-guide-with-15-awesome-examples/
Subscribe to:
Posts (Atom)