Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jquery .keyup .keydown .keypress don't detect browser autofill

I am using these events to try to evaluate form input, but if the form is autofilled by the browser these events don't fire. How can I make the event fire on autofill as well?

like image 306
A_funs Avatar asked May 16 '12 01:05

A_funs


2 Answers

I know it's quite old question, but I solved the same problem using e.preventDefault() after the keyup function.

$("#email").keyup(function (e)  {
        e.preventDefault();

In this way if you select with enter the suggestion by the browser autofill it will notice. The problem still remains when you click with the mouse on the suggestion, but it works fine if you press any key after selected it.

FINAL SOLUTION

I found this post on stackoverflow: https://stackoverflow.com/a/24746108/3481005

and using .on("input") does the trick!

$("#email").on("input",function (e)  {
like image 117
myh34d Avatar answered Nov 10 '22 01:11

myh34d


There is no way to detect a browser autofill, unfortunately. Instead, use setInterval, for example, to run a function at a fairly high frequency that checks whether the original value (or empty) matches the input value and trigger the response that way. If you don't want to do that, you're probably SOL since the browser will not even trigger .change, and any event is most likely not cross-browser compliant.

You could also have the validation events run on form submission as well, which should be just as effective.

like image 42
Explosion Pills Avatar answered Nov 09 '22 23:11

Explosion Pills