Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google's autocomplete not activated by pasting with mouse

The Google autocomplete API doesn't seem to be activating by pasting content into a text input with the mouse. It works fine if involving the keyboard at all, but not with just mouse.

I did notice, however, that after you paste your content into the text input it will activate from almost any keypress (tested right arrow key, end key, space).

You can repro it here on their autocomplete demo site.

Is this a bug? or as designed? If it's as designed, how to apply workaround? I've got this as a workaround so far, but no simulated keypress events seem to work.

$('.txtLocation').bind("paste", function (e)
{
    $('.txtLocation').focus();
    var e = jQuery.Event("keydown");
    e.keyCode = 39; //39=Arrow Right
    $('.txtLocation').trigger(e);
});
like image 775
johntrepreneur Avatar asked Nov 10 '12 20:11

johntrepreneur


1 Answers

It seems this impacts not only the context-menu Paste, but also that of Edit|Paste from the browser menu bar as well as the iOS paste functionality. I've opened a bug with Google. You may wish to "Star" that bug report to catch updates.

I found a workaround that, while a bit of a hack, seems to fix the problem. If you store the pasted value, switch focus on a different field, set the value in the Autocomplete field, and finally focus back on the Autocomplete field things work more or less as expected. Also, you have to do this in a setTimeout() callback - the delay time doesn't seem to matter at all, but if you just do this inline you won't see the expected results.

Here's a code sample of what I'm describing above:

$("#address_field").on("paste", googleMapsAutocompletePasteBugFix);

googleMapsAutocompletePasteBugFix = function() {
    return setTimeout(function() {
        var field, val;
        field = $("#address_field");
        val = field.val();
        $("#price").focus();
        field.val(val);
        return field.focus();
    }, 1);
};

The last focus() is optional, but the UI is a little less surprising than if you just skipped automatically to the next field.

like image 52
Ryan Dlugosz Avatar answered Oct 12 '22 19:10

Ryan Dlugosz