Article

Make Life Easy With Autocomplete Textboxes

Page: 1 2 3

The important first step is to ascertain the character the user typed (using String.fromCharCode() on the event object’s keyCode attribute (in IE) and charCode attribute (in Mozilla)). We use that character to replace the currently selected text. Then, we need to get the length of the text in the textbox.

function autocomplete(oTextbox, oEvent, arrValues) {  
 
   switch (oEvent.keyCode) {  
       case 38: //up arrow    
       case 40: //down arrow  
       case 37: //left arrow  
       case 39: //right arrow  
       case 33: //page up    
       case 34: //page down    
       case 36: //home    
       case 35: //end                    
       case 13: //enter    
       case 9: //tab    
       case 27: //esc    
       case 16: //shift    
       case 17: //ctrl    
       case 18: //alt    
       case 20: //caps lock  
       case 8: //backspace    
       case 46: //delete  
           return true;  
           break;  
 
 
       default:  
           textboxReplaceSelect(oTextbox, String.fromCharCode(isIE ? oEvent.keyCode : oEvent.charCode);  
           var iLen = oTextbox.value.length;  
           ...
 
   }  
}

Next, we need to search for a matching value in the array of values using our autocompleteMatch() method.

function autocomplete(oTextbox, oEvent, arrValues) {  
 
   switch (oEvent.keyCode) {  
       case 38: //up arrow    
       case 40: //down arrow  
       case 37: //left arrow  
       case 39: //right arrow  
       case 33: //page up    
       case 34: //page down    
       case 36: //home    
       case 35: //end                    
       case 13: //enter    
       case 9: //tab    
       case 27: //esc    
       case 16: //shift    
       case 17: //ctrl    
       case 18: //alt    
       case 20: //caps lock  
       case 8: //backspace    
       case 46: //delete  
           return true;  
           break;  
 
       default:  
           textboxReplaceSelect(oTextbox, String.fromCharCode(isIE ? oEvent.keyCode : oEvent.charCode);  
           var iLen = oTextbox.value.length;  
 
           var sMatch = autocompleteMatch(oTextbox.value, arrValues);  
 
           ...  
   }  
}

After we request a matching value, we need to determine if a match has indeed been found. To do this, we test sMatch to see if it’s null. If it is not null, we need to replace the text in the textbox with sMatch. Then we’ll use iLen (the length of the text actually typed by the user) to select only the text that the user hasn’t typed with the help of our textboxSelect() method.

function autocomplete(oTextbox, oEvent, arrValues) {  
 
   switch (oEvent.keyCode) {  
       case 38: //up arrow    
       case 40: //down arrow  
       case 37: //left arrow  
       case 39: //right arrow  
       case 33: //page up    
       case 34: //page down    
       case 36: //home    
       case 35: //end                    
       case 13: //enter    
       case 9: //tab    
       case 27: //esc    
       case 16: //shift    
       case 17: //ctrl    
       case 18: //alt    
       case 20: //caps lock  
       case 8: //backspace    
       case 46: //delete  
           return true;  
           break;  
 
       default:  
           textboxReplaceSelect(oTextbox, String.fromCharCode(isIE ? oEvent.keyCode : oEvent.charCode);  
           var iLen = oTextbox.value.length;  
 
           var sMatch = autocompleteMatch(oTextbox.value, arrValues);  
 
           if (sMatch != null) {  
               oTextbox.value = sMatch;  
               textboxSelect(oTextbox, iLen, oTextbox.value.length);  
           }
 
             
           ...  
   }  
}

After all this, the last thing we need to do is return the value of false to the event handler. If we don’t do this, the typed character will appear twice.

function autocomplete(oTextbox, oEvent, arrValues) {  
 
   switch (oEvent.keyCode) {  
       case 38: //up arrow    
       case 40: //down arrow  
       case 37: //left arrow  
       case 39: //right arrow  
       case 33: //page up    
       case 34: //page down    
       case 36: //home    
       case 35: //end                    
       case 13: //enter    
       case 9: //tab    
       case 27: //esc    
       case 16: //shift    
       case 17: //ctrl    
       case 18: //alt    
       case 20: //caps lock  
       case 8: //backspace    
       case 46: //delete  
           return true;  
           break;  
 
       default:  
           textboxReplaceSelect(oTextbox, String.fromCharCode(isIE ? oEvent.keyCode : oEvent.charCode);  
           var iLen = oTextbox.value.length;  
 
           var sMatch = autocompleteMatch(oTextbox.value, arrValues);  
 
           if (sMatch != null) {  
               oTextbox.value = sMatch;  
               textboxSelect(oTextbox, iLen, oTextbox.value.length);  
           }    
             
           return false;  
   }  
}

Example

The final step, of course, is to make an example that tests the code. Without further ado, here it is:

<html>  
<head>  
 <title>Autocomplete Textbox Example</title>  
       <script type="text/javascript">  
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;  
           /* falls through */  
             
       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, arrValues) {  
 
   for (var i=0; i < arrValues.length; i++) {  
       if (arrValues[i].indexOf(sText) == 0) {  
           return arrValues[i];  
       }  
   }  
 
   return null;  
 
}  
 
function autocomplete(oTextbox, oEvent, arrValues) {  
 
   switch (oEvent.keyCode) {  
       case 38: //up arrow    
       case 40: //down arrow  
       case 37: //left arrow  
       case 39: //right arrow  
       case 33: //page up    
       case 34: //page down    
       case 36: //home    
       case 35: //end                    
       case 13: //enter    
       case 9: //tab    
       case 27: //esc    
       case 16: //shift    
       case 17: //ctrl    
       case 18: //alt    
       case 20: //caps lock  
       case 8: //backspace    
       case 46: //delete  
           return true;  
           break;  
 
       default:  
           textboxReplaceSelect(oTextbox, String.fromCharCode(isIE ? oEvent.keyCode : oEvent.charCode);  
           var iLen = oTextbox.value.length;  
 
           var sMatch = autocompleteMatch(oTextbox.value, arrValues);  
 
           if (sMatch != null) {  
               oTextbox.value = sMatch;  
               textboxSelect(oTextbox, iLen, oTextbox.value.length);  
           }    
             
           return false;  
   }  
}  
       </script>  
       <script>  
               var arrValues = ["red", "orange", "yellow", "green", "blue", "indigo", "violet", "brown"];  
       </script>  
</head>  
<body>  
<h2>Autocomplete Textbox Example</h2>  
<p>Type in a color in lowercase:<br />  
<input type="text" value="" id="txt1" onkeypress="return autocomplete(this, event, arrValues)" /></p>  
</body>  
</html>

Wrap-up

Users like to be able to type values in instead of using the mouse, and an autocomplete textbox will make them very happy.

The only limitation to this code is that it is case sensitive, though the autocompleteMatch() function can easily be changed to make it case insensitive (I’ll leave you to figure out exactly how!). This code will not work in the Netscape Navigator 4.x family, or on Opera. Its functionality is unknown (but assumed not to work) in other browsers.

If you liked this article, share the love:
Print-Friendly Version Suggest an Article

Sponsored Links

Rate This Article

  • 1
    Poor
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
    Great

Comment on This Article

Have something to say?

Post A Comment

You need to be a member of the SitePoint Forums to comment on this post. Sign Up

Already a member? Post using your SitePoint Forums account:

Follow SitePoint on...