views:

240

answers:

3

I'm using an auto-complete widget from YUI to implement live search as in the examples. However, it works fine when search text is typed in, but fails to work when the text is pasted into the field. Which would be the proper way to initiate an autocompletion on paste? Haven't found anything for that in the documentation...

EDIT: Pasting is not Ctrl-V, it's usually "Paste" from the context-menu. YUI does react to a keypress, but doesn't if anything is pasted by the mouse.

A: 

Perhaps on key event, you could detect if they pressed v while holding ctrl. If they have, then do a sendQuery('query=' + textInput.value);

Zoidberg
A: 

Edit

Here's a compatibility table showing which browsers let you subscribe to the paste events.

http://www.quirksmode.org/dom/events/cutcopypaste.html

Here's his testing page where you can see how to subscribe to the events.

http://www.quirksmode.org/dom/events/tests/cutcopypaste.html

I imagine you could subscribe to that using YUI & then just have your callback be this:

function() { 
    autoCompleteObject.sendQuery(autoCompleteElement.value);
}

Watch out for browser incompatibilities, looks like some have a weird implementation of the events.

Tivac
+2  A: 

We have extended YUI's autocomplete widget and handle paste from the context menu in this way:

YAHOO.util.Event.on(input, 'paste', function(e, autocomplete) {
    // We're interested in the value of the input field after text is pasted into
    // it instead of the pasted text because the autocomplete proposals are based 
    // upon the field's whole value. The paste event happens before the input 
    // field has been updated so we need to wait until after this event has been 
    // handled to check the value of the input field.
    window.setTimeout(function() {
        if (autocomplete._sInitInputValue !== autocomplete.getInputEl().value) {
            autocomplete.sendQuery(autocomplete.getInputEl().value);
        }
    }, 1);
}, this);

Where this is the autocomplete widget.

wrumsby