Skip to content

Instantly share code, notes, and snippets.

@wmandrews
Created October 11, 2012 20:50
Show Gist options
  • Save wmandrews/3875394 to your computer and use it in GitHub Desktop.
Save wmandrews/3875394 to your computer and use it in GitHub Desktop.
jQuery UI Combobox
<script type="text/javascript" src="http://www.washingtonpost.com/wp-srv/special/politics/capitol-assets/wave3/templates/js/jquery-ui-1.8.23.custom.min.js"></script>
<script>
(function($){
$(document).ready(function() {
$('#states').combobox();
$('#stateNav input').focus(function() {
if (!$(this).hasClass('clicked')) {
$(this).addClass('clicked').val('');
}
}).val('Jump to a state');
$('#stateNav').fadeIn();
});
function runFilter(val) {
// do whatever you want with the val here
}
$.widget( "ui.combobox", {
_create: function() {
var input,
self = this,
select = this.element.hide(),
selected = select.children( ":selected" ),
value = selected.val() ? selected.text() : "",
wrapper = this.wrapper = $( "<span>" )
.addClass( "ui-combobox" )
.insertAfter( select );
input = $( "<input>" )
.appendTo( wrapper )
.val( value )
.addClass( "ui-state-default ui-combobox-input" )
.autocomplete({
delay: 0,
minLength: 0,
source: function( request, response ) {
var matcher = new RegExp( $.ui.autocomplete.escapeRegex(request.term), "i" );
response( select.children( "option" ).map(function() {
var text = $( this ).text();
if ( this.value && ( !request.term || matcher.test(text) ) )
return {
label: text.replace(
new RegExp(
"(?![^&;]+;)(?!<[^<>]*)(" +
$.ui.autocomplete.escapeRegex(request.term) +
")(?![^<>]*>)(?![^&;]+;)", "gi"
), "<strong>$1</strong>" ),
value: text,
option: this
};
}) );
},
select: function( event, ui ) {
ui.item.option.selected = true;
self._trigger( "selected", event, {
item: ui.item.option
});
runFilter(ui.item.value);
},
change: function( event, ui ) {
if ( !ui.item ) {
var matcher = new RegExp( "^" + $.ui.autocomplete.escapeRegex( $(this).val() ) + "$", "i" ),
valid = false;
select.children( "option" ).each(function() {
if ( $( this ).text().match( matcher ) ) {
this.selected = valid = true;
return false;
}
});
if ( !valid ) {
// remove invalid value, as it didn't match anything
$( this ).val( "" );
select.val( "" );
input.data( "autocomplete" ).term = "";
return false;
}
}
}
})
.addClass( "ui-widget ui-widget-content ui-corner-left" );
input.data( "autocomplete" )._renderItem = function( ul, item ) {
return $( "<li></li>" )
.data( "item.autocomplete", item )
.append( "<a>" + item.label + "</a>" )
.appendTo( ul );
};
$( "<a>" )
.attr( "tabIndex", -1 )
.attr( "title", "Show All Items" )
.appendTo( wrapper )
.button({
icons: {
primary: "ui-icon-triangle-1-s"
},
text: false
})
.removeClass( "ui-corner-all" )
.addClass( "ui-corner-right ui-combobox-toggle" )
.click(function() {
// close if already visible
if ( input.autocomplete( "widget" ).is( ":visible" ) ) {
input.autocomplete( "close" );
return;
}
// work around a bug (likely same cause as #5265)
$( this ).blur();
// pass empty string as value to search for, displaying all results
input.autocomplete( "search", "" );
input.focus();
});
},
destroy: function() {
this.wrapper.remove();
this.element.show();
$.Widget.prototype.destroy.call( this );
}
});
function slugify(val) {
var newText = val.replace(/ /g,'-').toLowerCase();
return newText;
}
})(jQuery);
</script>
<link type="text/css" rel="stylesheet" href="http://media.washingtonpost.com/wp-srv/special/politics/capitol-assets/wave3/templates/css/jquery-ui-1.8.23.custom.css" charset="utf-8">
<style>
/* autocomplete */
.wp-conflicts .ui-corner-all, .wp-conflicts .ui-corner-top, .wp-conflicts .ui-corner-left, .wp-conflicts .ui-corner-right, .wp-conflicts .ui-corner-bottom, .wp-conflicts .ui-corner-tl, .wp-conflicts .ui-corner-tr, .wp-conflicts .ui-corner-br, .wp-conflicts .ui-corner-bl { -moz-border-radius: 0px; -webkit-border-radius: 0px; -khtml-border-radius: 0px; border-radius: 0px; }
.wp-conflicts .ui-autocomplete {text-align: left;max-height: 114px;overflow-y: scroll;max-width:195px;}
.wp-conflicts .ui-autocomplete a {color: #222;display: block;font-size: 12px;padding:5px 4px;border:0 none;font-weight: normal;}
.wp-conflicts .ui-autocomplete a:hover, .wp-conflicts .ui-autocomplete a.ui-state-hover {border:0 none;background: #e2e2e2;color: black;font-weight: normal;margin:0;}
#stateNav { position: relative; display: none; }
#stateNav input { height:22px; padding-top:1px; line-height:18px; font-size: 13px; color: #ccc; width: 120px; border:1px solid #CCC; background:white; color:#222; padding:2px 6px 1px;}
#stateNav .ui-button {background:white;position: absolute;right:5px;top:5px;}
</style>
<div id="stateNav">
<select id="states">
<option value="">Jump to a state</option>
<option value="Alabama">Alabama</option>
<option value="Alaska">Alaska</option>
<option value="Arizona">Arizona</option>
<option value="Arkansas">Arkansas</option>
</select>
</div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment