Ecchime qua,
ho la soluzione, spero possa essere utile a qualcuno..
codice:
var isOpera = navigator.userAgent.indexOf("Opera") > -1;
var isIE = navigator.userAgent.indexOf("MSIE") > 1 && !isOpera;
var isMoz = navigator.userAgent.indexOf("Mozilla/5.") == 0 && !isOpera;
function textboxSelect ( oTextbox, iStart, iEnd ) {
switch(arguments.length) {
case 1: oTextbox.select(); break;
case 2: iEnd = oTextbox.value.length;
case 3:
if (isIE) {
var oRange = oTextbox.createTextRange();
oRange.moveStart("character", iStart);
oRange.moveEnd("character", -oTextbox.value.length + iEnd);
oRange.select();
} else if (isMoz) oTextbox.setSelectionRange(iStart, iEnd);
}
oTextbox.focus();
}
function textboxReplaceSelect ( oTextbox, sText ) {
if (isIE) {
var oRange = document.selection.createRange();
oRange.text = sText;
oRange.collapse(true);
oRange.select();
} else if (isMoz) {
var iStart = oTextbox.selectionStart;
oTextbox.value = oTextbox.value.substring(0, iStart) + sText + oTextbox.value.substring(oTextbox.selectionEnd, oTextbox.value.length);
oTextbox.setSelectionRange(iStart + sText.length, iStart + sText.length);
}
oTextbox.focus();
}
function autocompleteMatch ( sText ) {
var arrValues = ["casa","cane","ciccio","miao","quello che te pare"];
for (var i=0; i < arrValues.length; i++) if (arrValues[i].indexOf(sText) == 0) return arrValues[i];
return null;
}
function autocomplete ( oTextbox, oEvent ) {
switch (oEvent.keyCode) {
case 8: return true; break; // Backspace
case 9: return true; break; // TAB
case 13: return true; break; // Invio
case 16: return true; break; // Shift
case 17: return true; break; // CTRL
case 18: return true; break; // ALT
case 20: return true; break; // Caps Lock
case 27: return true; break; // Esc
case 33: return true; break; // Pagina Su
case 34: return true; break; // Pagina Giù
case 35: return true; break; // Fine
case 36: return true; break; // Home
case 37: return true; break; // Freccia Sinistra
case 38: return true; break; // Freccia Su
case 39: return true; break; // Freccia Destra
case 40: return true; break; // Freccia Giù
case 46: return true; break; // Canc
default:
textboxReplaceSelect(oTextbox, String.fromCharCode(isIE ? oEvent.keyCode : oEvent.charCode));
var iLen = oTextbox.value.length;
var sMatch = autocompleteMatch(oTextbox.value);
if (sMatch != null) {
oTextbox.value = sMatch;
textboxSelect(oTextbox, iLen, oTextbox.value.length);
}
return false;
break;
}
}
il campo dev'essere così:
codice:
<input type="text" value="" id="txt1" onKeyPress="return autocomplete(this, event)" autocomplete="off">
il tutto è compatibile anche con Firefox Mozilla...
Ciao