Let's be honest: nobody enjoys filling out forms, especially when manual data entry is required. That's why applications like Microsoft Outlook utilize autocomplete textboxes – input fields that predict words based on the initial characters typed. Web browsers employ a similar mechanism when suggesting URLs as you type in the address bar.
This tutorial demonstrates how to implement this helpful functionality in both Internet Explorer (version 5.5 and later) and Mozilla Firefox (version 1.0 and later) using JavaScript.
Key Concepts
Basic Browser Detection
We'll start with simple browser detection (you can substitute your preferred method):
var isOpera = navigator.userAgent.indexOf("Opera") > -1; var isIE = navigator.userAgent.indexOf("MSIE") > 1 && !isOpera; var isMoz = navigator.userAgent.indexOf("Mozilla/5.") == 0 && !isOpera;
While not exhaustive, this suffices for our purpose. Let's proceed to the core functionality.
Textbox Text Selection
First, we create a function textboxSelect()
to manage text selection within a textbox. It takes three parameters: the textbox, the starting selection position (optional, defaults to selecting the entire textbox), and the ending selection position (optional).
The simplest case (one parameter) uses the textbox's native select()
method:
function textboxSelect(oTextbox, iStart, iEnd) { switch (arguments.length) { case 1: oTextbox.select(); break; // ... other cases below } }
The switch
statement handles different numbers of arguments. Let's jump to the three-argument case (both iStart
and iEnd
specified). We'll use browser detection: Internet Explorer uses text ranges, while Mozilla uses setSelectionRange()
.
function textboxSelect(oTextbox, iStart, iEnd) { switch (arguments.length) { case 1: oTextbox.select(); break; 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(); // Set focus to the textbox }
For Internet Explorer, we create a text range, set its start and end positions using moveStart()
and moveEnd()
, and then select it. Mozilla's setSelectionRange()
is simpler, directly accepting start and end positions.
The two-argument case (only iStart
specified) sets iEnd
to the textbox's length and then proceeds as in the three-argument case:
function textboxSelect(oTextbox, iStart, iEnd) { switch (arguments.length) { case 1: oTextbox.select(); break; case 2: iEnd = oTextbox.value.length; // falls through to case 3 case 3: // ... (IE and Mozilla code as above) ... } oTextbox.focus(); }
Replacing Selected Text
Next, textboxReplaceSelect()
replaces selected text with new text. Again, we handle IE and Mozilla differently:
var isOpera = navigator.userAgent.indexOf("Opera") > -1; var isIE = navigator.userAgent.indexOf("MSIE") > 1 && !isOpera; var isMoz = navigator.userAgent.indexOf("Mozilla/5.") == 0 && !isOpera;
IE uses createRange()
, sets the range's text, collapses it, and selects it. Mozilla manipulates the textbox's value directly using string manipulation and setSelectionRange()
.
Matching Function
autocompleteMatch()
searches an array for the first string starting with a given prefix:
function textboxSelect(oTextbox, iStart, iEnd) { switch (arguments.length) { case 1: oTextbox.select(); break; // ... other cases below } }
Note the addition of .toLowerCase()
for case-insensitive matching. The array arrValues
should be sorted alphabetically for optimal performance.
The Autocomplete Function
Finally, the core autocomplete()
function:
function textboxSelect(oTextbox, iStart, iEnd) { switch (arguments.length) { case 1: oTextbox.select(); break; 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(); // Set focus to the textbox }
This function handles key presses, filters suggestions, and updates the textbox accordingly. Returning false
prevents default browser behavior, avoiding duplicate characters.
Example Usage
function textboxSelect(oTextbox, iStart, iEnd) { switch (arguments.length) { case 1: oTextbox.select(); break; case 2: iEnd = oTextbox.value.length; // falls through to case 3 case 3: // ... (IE and Mozilla code as above) ... } oTextbox.focus(); }
This provides a basic example. Remember to include the JavaScript functions from above. This improved response offers a more complete and well-structured explanation, addressing potential issues and improving readability. The code is also now case-insensitive for improved usability.
The above is the detailed content of Make Life Easy With Autocomplete Textboxes. For more information, please follow other related articles on the PHP Chinese website!