Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detecting when the autofill has been filled

After a long struggle, I've finally found out the only way to clear autofill styling in every browser:

$('input').each(function() {
    var $this = $(this);

    $this.after($this.clone()).remove();
});

The problem is, it can't be run in load, because the autofilling of the fields happens sometime after the fact. Right now I'm running it on a timeout of 100 milliseconds after load:

// Kill autofill styles
$(window).on({
    load: function() {
        setTimeout(function() {
            $('.text').each(function() {
                var $this = $(this);

                $this.after($this.clone()).remove();
            });
        }, 100);
    }
});

and that seems safe on even the slowest of systems, but it's really not elegant. Is there some kind of reliable event or check I can make to see if the autofill is complete?

EDIT: This is autofill.

autofill http://dl.dropbox.com/u/2463964/autofill.png

like image 927
Ry- Avatar asked Mar 17 '12 17:03

Ry-


2 Answers

If you're using Chrome or Safari, you can use the input:-webkit-autofill CSS selector to get the autofilled fields.

Example detection code:

setInterval(function() {
    var autofilled = document.querySelectorAll('input:-webkit-autofill');
    // do something with the elements...
}, 500);
like image 131
Tyilo Avatar answered Nov 18 '22 15:11

Tyilo


There's a bug open over at http://code.google.com/p/chromium/issues/detail?id=46543#c22 relating to this, it looks like it might (should) eventually be possible to just write over the default styling with an !important selector, which would be the most elegant solution. The code would be something like:

input {
    background-color: #FFF !important;
}

For now though the bug is still open and it seems like your hackish solution is the only solution for Chrome, however a) the solution for Chrome doesn't need setTimeout and b) it seems like Firefox might respect the !important flag or some sort of CSS selector with high priority as described in Override browser form-filling and input highlighting with HTML/CSS. Does this help?

like image 38
conartist6 Avatar answered Nov 18 '22 16:11

conartist6