Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery - turning "autocomplete" to off for all forms (even ones not loaded yet)

Tags:

jquery

forms

So, I've got this code:

$(document).ready(function(){
    $('form').attr('autocomplete', 'off');
});

It works great for all forms already existing. The problem is, some forms of mine are in pop ups loaded throught ajax. This won't apply to them since they're "loaded" later.

I know there's a live() function - but that's only for attaching events. What's a good way to apply this to all forms?

Thanks.

like image 950
Matthew Avatar asked May 18 '10 20:05

Matthew


People also ask

How do I force autocomplete off?

To disable the autocomplete of text in forms, use the autocomplete attribute of <input> and <form> elements. You'll need the "off" value of this attribute.

How do I stop autofill in Chrome using jQuery?

Autocomplete attribute of HTML is use to fetch previous input values in input fields. Almost all browsers supports autocomplete attribute, by default this attribute is “on” in html. If you want to disable this attribute ,you can set autocomplete=”off” in html input tag.

How do I disable input field autocomplete?

Add autocomplete="off" onto <form> element; Add hidden <input> with autocomplete="false" as a first children element of the form.

Is autocomplete on by default?

Autocomplete is a default feature of most modern web browsers. It anticipates what you are typing and suggests a word or phrase based on the activity of other users and your history. If you press Enter , the application automatically completes your typing with the remainder of the suggested text.


1 Answers

You could bind a live event to the focus event and then change the attribute on that.

$(document).ready(function(){
    $(':input').live('focus',function(){
        $(this).attr('autocomplete', 'off');
    });
});

As of jQuery 1.7 jQuery.live was deprecated. This code would now look like:

$(document).ready(function(){
    $( document ).on( 'focus', ':input', function(){
        $( this ).attr( 'autocomplete', 'off' );
    });
});
like image 53
PetersenDidIt Avatar answered Oct 20 '22 00:10

PetersenDidIt