jQuery & jQueryUI Base
HTML
<link rel="stylesheet" href="http://ajax.microsoft.com/ajax/jquery.ui/1.8.7/themes/black-tie/jquery-ui.css">
<script src="http://ajax.aspnetcdn.com/ajax/jquery.ui/1.8.7/jquery-ui.min.js"></script>
<input type="text" id="autocomplete" />
JavaScript
$("#autocomplete").autocomplete({
source: [
"ActionScript",
"AppleScript",
"Asp",
"BASIC",
"C",
"C++",
"Clojure",
"COBOL",
"ColdFusion",
"Erlang",
"Fortran",
"Groovy",
"Haskell",
"Java",
"JavaScript",
"Lisp",
"Perl",
"PHP",
"Python",
"Ruby",
"Scala",
"Scheme"
],
focus: function() { return false; },
open: function( event, ui ) {
var firstElement = $(this).data("autocomplete").menu.element[0].children[0]
, inpt = $('#autocomplete')
, original = inpt.val()
, firstElementText = $(firstElement).text();
/*
here we want to make sure that we're not matching something that doesn't start
with what was typed in
*/
if(firstElementText.toLowerCase().indexOf(original.toLowerCase()) === 0){
inpt.val(firstElementText);//change the input to the first match
inpt[0].selectionStart = original.length; //highlight from end of input
inpt[0].selectionEnd = firstElementText.length;//highlight to the end
}
}
});