11

jQuery UI Autocomplete Combobox: Pick 1st item with enter key

Posted März 21st, 2012 in javascript by ov3rk1ll

So, I was using the jQuery UI autocomplete module with the provided example code from http://jqueryui.com/demos/autocomplete/#combobox.

Sadly, I this example, pressing the return/enter key is doing nothing so I added a few lines of code to make the enter key select the 1st entry from the autocomplete box and trigger the select-element’s change event.

The code I added to the input field:


.keypress(function(event){ if (event.which == 13 && $('ul.ui-autocomplete').is(':visible')){
var li = $('li.ui-menu-item:first')[0];
var item = $(li).data("item.autocomplete");
input.data("autocomplete").options.select(event, {item: item});
}
})

The whole script with my code


(function( $ ) {
$.widget( "ui.combobox", {
_create: function() {
var self = this,
select = this.element.hide(),
selected = select.children( ":selected" ),
value = selected.val() ? selected.text() : "";
var input = this.input = $( "<input>" )
.insertAfter( select )
.val( value )
.keypress(function(event){ if (event.which == 13 && $('ul.ui-autocomplete').is(':visible')){
var li = $('li.ui-menu-item:first')[0];
var item = $(li).data("item.autocomplete");
input.data("autocomplete").options.select(event, {item: item});
}
})
.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
});
select.trigger("change");
},
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 )
};

this.button = $( '<button type="button" data-role="none">&nbsp;</button>' )
.attr( "tabIndex", -1 )
.attr( "title", "Show All Items" )
.insertAfter( input )
.button({
icons: {
primary: "ui-icon-triangle-1-s"
},
text: false
})
.removeClass( "ui-corner-all" )
.addClass( "ui-corner-right ui-button-icon" )
.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.input.remove();
this.button.remove();
this.element.show();
$.Widget.prototype.destroy.call( this );
}
});
})( jQuery );

I hope this is useful to somebody!

 

11 Responses so far.

  1. lina evelin sagt:

    ajarin bikin webset dong

  2. henry sagt:

    geez, ever heard of code indentation?

  3. Kandhasamy sagt:

    Oru Sandhu vil Kandhu…

  4. Alex Forch sagt:

    I have to tell you that it’s hard to find your posts
    in google, i found this one on 15 spot, you should build some quality backlinks in order to rank your site, i know how to help you, just search in google –
    k2 seo tips and tricks

  5. Heather sagt:

    I read a lot of interesting articles here. Probably you spend a lot of time writing, i know how to save you a lot
    of time, there is an online tool that creates readable, SEO
    friendly articles in seconds, just search in google – laranitas free content source

  6. Kako Ramalho sagt:

    Indented

    (function( $ )
    {
    $.widget( “ui.combobox”,
    {
    _create: function()
    {
    var self = this,
    select = this.element.hide(),
    selected = select.children( “:selected” ),
    value = selected.val() ? selected.text() : “”;
    var input = this.input = $( “” )
    .insertAfter( select )
    .val( value )
    .keypress(function(event)
    {
    if (event.which == 13 && $(‘ul.ui-autocomplete’).is(‘:visible’))
    {
    var li = $(‘li.ui-menu-item:first’)[0];
    var item = $(li).data(“item.autocomplete”);
    input.data(“autocomplete”).options.select(event, {item: item});
    }
    })

    .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”
    ), “$1” ),
    value: text,
    option: this
    };
    }) );
    },
    select: function( event, ui )
    {
    ui.item.option.selected = true;

    self._trigger( “selected”, event,
    {
    item: ui.item.option
    });

    select.trigger(“change”);
    },
    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 $( “” )
    .data( “item.autocomplete”, item )
    .append( “” + item.label + “” )
    .appendTo( ul )
    };

    this.button = $( ‘ ’ )
    .attr( “tabIndex”, -1 )
    .attr( “title”, “Show All Items” )
    .insertAfter( input )
    .button(
    {
    icons:
    {
    primary: “ui-icon-triangle-1-s”
    },
    text: false
    })

    .removeClass( “ui-corner-all” )
    .addClass( “ui-corner-right ui-button-icon” )
    .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.input.remove();
    this.button.remove();
    this.element.show();
    $.Widget.prototype.destroy.call( this );
    }
    });
    })( jQuery );

  7. weight loss sagt:

    Interesting posts you post here, i have shared this article on my fb

  8. Ofelia sagt:

    If you are interested in topic: earn online money free kesha story – you should read about Bucksflooder first

Leave a Reply