user509423
user509423

Reputation:

YUI AutoComplete events, how to?

I'm using YUI 3.3.0 and the AutoComplete widget. I'm entirely new to YUI. Here's the thing. I have AutoComplete working.

How do I catch an event fired by AutoComplete? The documentation states that a select event is fired when a user selects an item from the list. I want to attach a function to that event. How do I do that?

Upvotes: 7

Views: 3469

Answers (1)

Tivac
Tivac

Reputation: 2573

Here's an example for the plugin approach, http://tivac.com/yui3/so/skladjfyhafjk_autocomplete.htm

Simply pass your event handlers as part of the config when you first plug autocomplete into the input.

Y.one("#ac").plug(Y.Plugin.AutoComplete, {
    resultHighlighter: 'phraseMatch',
    source: ['foo', 'bar', 'baz'],
    on : {
        select : function(e) {
            console.log(arguments); //TODO: REMOVE DEBUGGING
        }
    }
});

You can also subscribe after the element has been plugged using the namespace it attaches to ("ac").

Y.one("#ac").ac.on("select", function() {
    console.log("post-plugin event subscription"); //TODO: REMOVE DEBUGGING
});

If you are using it as a class, it works like this.

var ac = new Y.AutoComplete({
    inputNode: '#ac',
    source: ['foo', 'bar', 'baz']
});

ac.on("select", function() {
    console.log("Class event subscription"); //TODO: REMOVE DEBUGGING
});

Upvotes: 16

Related Questions