input tags
field
web form
autocomplete
browser

How do you disable browser autocomplete on web form field / input tags?

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

To disable browser autocomplete on form fields or <input> tags, you can use the autocomplete attribute with the value off. While most browsers honor this setting, there are a few nuances to consider.


1. Basic Solution: Add autocomplete="off"

You can add the autocomplete="off" attribute to individual form fields or the entire <form> element.

Example 1: Disabling Autocomplete for the Entire Form

html
1<form action="/submit" method="post" autocomplete="off">
2    <label for="username">Username:</label>
3    <input type="text" id="username" name="username">
4
5    <label for="password">Password:</label>
6    <input type="password" id="password" name="password">
7
8    <button type="submit">Submit</button>
9</form>
  • Adding autocomplete="off" to the <form> disables autocomplete for all child input fields.

Example 2: Disabling Autocomplete for Specific Fields

You can disable autocomplete only for specific input fields.

html
1<form action="/submit" method="post">
2    <label for="username">Username:</label>
3    <input type="text" id="username" name="username" autocomplete="off">
4
5    <label for="email">Email:</label>
6    <input type="email" id="email" name="email">
7
8    <button type="submit">Submit</button>
9</form>
  • In this case, autocomplete is disabled only for the username field, but it remains enabled for the email field.

2. For Modern Browsers: Use autocomplete="new-password"

Some modern browsers may ignore autocomplete="off", particularly for password managers. Instead, use autocomplete="new-password" for fields like passwords or sensitive input.

Example: Password Field

html
1<form action="/submit" method="post">
2    <label for="password">Password:</label>
3    <input type="password" id="password" name="password" autocomplete="new-password">
4</form>
  • autocomplete="new-password" tricks browsers into not autofilling the password field, as they think it's asking for a new password.

3. Adding Dummy Input Fields (Workaround)

Some browsers may still auto-fill login forms despite autocomplete="off". A common workaround is to add a dummy hidden input field before the actual input.

Example: Dummy Input

html
1<form action="/submit" method="post" autocomplete="off">
2    <!-- Hidden dummy input field -->
3    <input type="text" name="fake_username" style="display: none;">
4    
5    <!-- Actual input fields -->
6    <label for="username">Username:</label>
7    <input type="text" id="username" name="username" autocomplete="off">
8
9    <label for="password">Password:</label>
10    <input type="password" id="password" name="password" autocomplete="new-password">
11
12    <button type="submit">Submit</button>
13</form>
  • The dummy field prevents autofill because the browser may fill the hidden field instead.

4. JavaScript-Based Solution

You can use JavaScript to clear the autocomplete behavior dynamically.

Example: Set autocomplete via JavaScript

html
1<form id="myForm">
2    <label for="username">Username:</label>
3    <input type="text" id="username" name="username">
4
5    <button type="submit">Submit</button>
6</form>
7
8<script>
9    document.getElementById('myForm').setAttribute('autocomplete', 'off');
10    document.getElementById('username').setAttribute('autocomplete', 'off');
11</script>

Browser Compatibility

  • Modern browsers like Chrome, Firefox, Safari, and Edge support autocomplete="off".
  • Some password managers or browsers may ignore autocomplete="off" for login forms due to user convenience features. Use autocomplete="new-password" for better control.

Summary

ApproachWhen to Use
autocomplete="off"General use; works for most form fields.
autocomplete="new-password"For password fields to prevent autofill.
Dummy hidden fieldsWorkaround for stubborn browsers.
JavaScript setAttribute()When dynamic control of autocomplete is needed.

Best Practice: Use autocomplete="off" for general cases and autocomplete="new-password" for sensitive fields like passwords. If necessary, add dummy fields or use JavaScript for stricter control. 🚀


Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions