Completing known words in a form field

I had a frequently-used form which contained a field, for tags, whose input was mostly coming from a list of 100 or so pre-existing values. Since I was already loading a list of the known tags to print them for my own reference, I figured I might as well let myself be partially replaced by automation.

I've tested this in Safari 4 and Firefox 3. Should work in IE as well.

Exhibit A: Working sample

Exhibit B: The code

function alphmatch(word, index, array) { return (word.substr(0,this.length) == this); } function complete(e) { if (!e) { var e = window.event; } //bah, kc = (e.keyCode || e.which); //compatibility humbug. if (kc < 48 || kc > 90) { // Only catch keyCodes for [0-9a-z]. return(null); } // Ignores numpad (keyCode 96-105), but feh. v = this.value.split(' '); v = v[v.length - 1]; optp = document.getElementById('intopts'); words = ['amusements','automation','apple','banana','baz','baz','cantaloupe','foo','LOL','quux'] wf = words.filter(alphmatch,v); optp.innerHTML = wf; if (wf.length == 1){ this.value += wf[0].substr(v.length); optp.innerHTML = ''; }} function go(){ //called onload. //document.getElementById("int").addEventListener("keyup",complete,false); document.getElementById("int").onkeyup = complete;}