Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using JQuery Autocomplete combobox

Im trying to use this JQuery code in my program: http://jqueryui.com/demos/autocomplete/#combobox

Its basically a combo-box which has a auto-complete field and a dropdown button.

When I try to use my combo box inside form tags, it doesnt work properly - the dropdown button keeps submitting the form when i just want to look up values.

The original code from the example is the following:

$( "<button>&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;
                }

                // pass empty string as value to search for, displaying all results
                input.autocomplete( "search", "" );
                input.focus();

            });

Any help appreciated :).

like image 868
Parampal Pooni Avatar asked Sep 11 '26 12:09

Parampal Pooni


1 Answers

I found the solution, just need to make two small changes:

.click(function() {
// close if already visible
if (input.autocomplete("widget").is(":visible")) {
    input.autocomplete("close");
    return false; // CHANGE 1
}
// pass empty string as value to search for, displaying all results
input.autocomplete("search", "");
input.focus();
return false; // CHANGE 2
});
like image 98
Parampal Pooni Avatar answered Sep 13 '26 05:09

Parampal Pooni



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!