»
EnglishFrenchVietnamese

Print - Auto-Complete Function on key press - JavaScriptBank.com

Full version: jsB@nk » Form » Auto-Complete Function on key press
URL: https://www.javascriptbank.com/auto-complete-function-on-key-press.html

Auto-Complete Function on key press © JavaScriptBank.comCreate an auto-complete or auto-suggest text box in JavaScript, without use of prototype handlers and other advanced techniques. This script could be adapted for use in a number of different situations.Maybe you also want to see our previous auto-complete scripts: Simple Auto-Complete and Auto-Complete Textfield.

Full version: jsB@nk » Form » Auto-Complete Function on key press
URL: https://www.javascriptbank.com/auto-complete-function-on-key-press.html



JavaScript
<script type="text/javascript" name="autoComplete.js">// Created by: Ilanio :: http://www.webdeveloper.com/forum/showthread.php?t=119753// Details can be found here:// http://www.webdeveloper.com/forum/showthread.php?t=119753var aMail = new Array("[email protected]","[email protected]","[email protected]","[email protected]","[email protected]", "[email protected]", "[email protected]", "[email protected]");aMail.sort();function Complete(obj, evt) { if ((!obj) || (!evt) || (aMail.length == 0)) {  return;  }  if (obj.value.length == 0) {  return;  }  var elm = (obj.setSelectionRange) ? evt.which : evt.keyCode;  if ((elm < 32) || (elm >= 33 && elm <= 46) || (elm >= 112 && elm <= 123)) {  return;  }  var txt = obj.value.replace(/;/gi, ",");  elm = txt.split(",");  txt = elm.pop();  txt = txt.replace(/^s*/, "");  if (txt.length == 0) {  return;  }  if (obj.createTextRange) {   var rng = document.selection.createRange();  if (rng.parentElement() == obj) {   elm = rng.text;   var ini = obj.value.lastIndexOf(elm);  }  } else if (obj.setSelectionRange) {  var ini = obj.selectionStart;  }  for (var i = 0; i < aMail.length; i++) {   elm = aMail[i].toString();  if (elm.toLowerCase().indexOf(txt.toLowerCase()) == 0) {   obj.value += elm.substring(txt.length, elm.length);   break;  }  }  if (obj.createTextRange) {  rng = obj.createTextRange();  rng.moveStart("character", ini);  rng.moveEnd("character", obj.value.length);  rng.select();  } else if (obj.setSelectionRange) {  obj.setSelectionRange(ini, obj.value.length);  }}</script>


HTML
<form name="anyForm">  <small>(Begin entering one of the names below)<br>  <em>Albert, Allen, Barry, Beth, Hal, Harry, Steve, Susan</em></small><br><br>  <strong>Send e-mail to:</strong>   <input type="text" name="anyName" onKeyUp="Complete(this, event)"></form>