Highlighting Autocomplete Text
Visually highlighting matched text in jQuery UI autocomplete menu items.
HTML
<div class="ui-widget">
<input placeholder="ui-state-highlight" />
</div>
<div class="ui-widget">
<input placeholder="bold-text" />
</div>
CSS
body {
font-size: 0.8em;
}
.bold-text {
font-weight: bold;
}
JavaScript
(function( $ ) {
// Custom autocomplete instance.
$.widget( "app.autocomplete", $.ui.autocomplete, {
// Which class get's applied to matched text in the menu items.
options: {
highlightClass: "ui-state-highlight"
},
_renderItem: function( ul, item ) {
// Replace the matched text with a custom span. This
// span uses the class found in the "highlightClass" option.
var re = new RegExp( "(" + this.term + ")", "gi" ),
cls = this.options.highlightClass,
template = "<span class='" + cls + "'>$1</span>",
label = item.label.replace( re, template ),
$li = $( "<li/>" ).appendTo( ul );
// Create and return the custom menu item content.
$( "<a/>" ).attr( "href", "#" )
.html( label )
.appendTo( $li );
return $li;
}
});
// Demo data for autocomplete source.
var tags = [
"ActionScript",
"AppleScript",
"Asp",
"BASIC",
"C",
"C++",
"Clojure",
"COBOL",
"ColdFusion",
"Erlang",
"Fortran",
"Groovy",
"Haskell",
"Java",
"JavaScript",
"Lisp",
"Perl",
"PHP",
"Python",
"Ruby",
"Scala",
"Scheme"
];
// Create autocomplete instances.
$(function() {
$( "input:first" ).autocomplete({
source: tags
});
$( "input:last" ).autocomplete({
highlightClass: "bold-text",
source: tags
});
});
})( jQuery );