alex89x
alex89x

Reputation: 479

Extend jQuery Autocomplete: override default behaviour on events

I'm developing an extension for the jQuery UI Autocomplete Plugin.

My code is approximatively as follows:

$.widget( "ui.autocomplete", $.ui.autocomplete, {
    options: {
        delay: 500,
        prefix: ""
    },

    _renderItem: function( ul, item ) {
        var label = item.label;
        if ( this.options.prefix ) {
            label = this.options.prefix + " " + label;
        }
        return $( "<li>" )
            .append( $( "<a>" ).text( label ) )
            .appendTo( ul );
    },
});

What I want to do is to extend the default behavior on EVENTS, like "Focus" (e.g. by using the following code):

focus: function() {
    // prevent value inserted on focus
    alert("fire");
    return false;
}

which at the moment I'm writing when I call my autocomplete()

$( "#search" ).autocomplete( { //focus:function() ...  })

Is there a way to do this? So that when I write

$( "#search" ).autocomplete()

automatically uses my pre-defined focus function within the widget?

Thanks!

Upvotes: 1

Views: 2539

Answers (1)

alex89x
alex89x

Reputation: 479

Ok the answer is as simple as that. You treat "focus", "select", "open" as any other option:

$.widget( "ui.autocomplete", $.ui.autocomplete, {
    options: {
        delay: 200,

        focus: function(event) {
            alert("does that");
        }
    }
});

That's it! Next time you call

$( "#search" ).autocomplete()

it will catch the focus!

Upvotes: 2

Related Questions