HTML Basic
HTML Reference
- HTML Tags
- HTML Deprecated Tags
- HTML Events
- HTML Attributes
- accept
- accept-charset
- accesskey
- action
- align
- alt
- as
- async
- autocomplete
- autofocus
- autoplay
- bgcolor
- border
- charset
- checked
- cite
- class
- color
- cols
- colspan
- content
- contenteditable
- controls
- coords
- data
- data-*
- datetime
- default
- defer
- dir
- dirname
- disabled
- download
- draggable
- enctype
- enterkeyhint
- for
- form
- formaction
- headers
- height
- hidden
- high
- href
- hreflang
- http-equiv
- id
- inert
- inputmode
- ismap
- kind
- label
- lang
- list
- loop
- low
- max
- maxlength
- media
- method
- min
- multiple
- muted
- name
- novalidate
- onabort
- onafterprint
- onbeforeprint
- onbeforeunload
- onblur
- oncanplay
- oncanplaythrough
- onchange
- onclick
- oncontextmenu
- oncopy
- oncuechange
- oncut
- ondblclick
- ondrag
- ondragend
- ondragenter
- ondragleave
- ondragover
- ondragstart
- ondrop
- ondurationchange
- onemptied
- onended
- onerror
- onfocus
- onhashchange
- oninput
- oninvalid
- onkeydown
- onkeypress
- onkeyup
- onload
- onloadeddata
- onloadedmetadata
- onloadstart
- onmousedown
- onmousemove
- onmouseout
- onmouseover
- onmouseup
- onmousewheel
- onoffline
- ononline
- onpagehide
- onpageshow
- onpaste
- onpause
- onplay
- onplaying
- onpopstate
- onprogress
- onratechange
- onreset
- onresize
- onscroll
- onsearch
- onseeked
- onseeking
- onselect
- onstalled
- onstorage
- onsubmit
- onsuspend
- ontimeupdate
- ontoggle
- onunload
- onvolumechange
- onwaiting
- onwheel
- open
- optimum
- pattern
- placeholder
- popover
- popovertarget
- popovertargetaction
- poster
- preload
- readonly
- rel
- required
- reversed
- rows
- rowspan
- sandbox
- scope
- selected
- shape
- size
- sizes
- span
- spellcheck
- src
- srcdoc
- srclang
- srcset
- start
- step
- style
- tabindex
- target
- title
- translate
- type
- usemap
- value
- width
- wrap
- HTML Global Attributes
- HTML Status Code
- HTML Language Code
- HTML Country Code
- HTML Charset
- MIME Types
HTML autocomplete Attribute
Photo Credit to CodeToFun
🙋 Introduction
The autocomplete
attribute is a handy feature in HTML that allows developers to control whether a browser should automatically complete user input in a form.
This attribute is applied to form elements and can be particularly useful for enhancing user experience and streamlining data entry.
🎯 Purpose of autocomplete
The primary purpose of the autocomplete
attribute is to specify whether a browser should provide autocomplete suggestions for a particular input field.
By default, most browsers have autocomplete enabled, which means they try to predict and fill in values based on the user's previous inputs.
However, there are situations where you might want to control or disable this behavior.
💎 Values
The autocomplete
attribute accepts various values to define different behaviors. The two main values are:
- on: This is the default value. It allows the browser to provide autocomplete suggestions for the input field.
- off: This value disables autocomplete for the input field. Use this when you want to prevent the browser from suggesting or remembering previous input.
📄 Example
Let's look at a simple example of how to use the autocomplete
attribute in an HTML form:
<form>
<label for="username">Username:</label>
<input type="text" id="username" name="username" autocomplete="off">
<label for="password">Password:</label>
<input type="password" id="password" name="password" autocomplete="on">
<input type="submit" value="Submit">
</form>
🧠 How it Works
In this example, the autocomplete
attribute is set to off for the username input field, indicating that autocomplete suggestions should be disabled for this specific field.
🔄 Dynamic Values with JavaScript
You can also dynamically set the autocomplete
attribute using JavaScript. This can be useful when you want to customize autocomplete behavior based on certain conditions or user interactions. Here's a brief example:
<script>
// Dynamically set autocomplete for an input field
document.getElementById("dynamicField").autocomplete = "new-password";
</script>
🧠 How it Works
In this script, the autocomplete
attribute is set to "new-password" for an input field with the id "dynamicField." This can be particularly helpful when dealing with password fields and wanting to prompt the browser to suggest a new password.
🏆 Best Practices
- Use the
autocomplete
attribute judiciously, considering the nature of the information being entered and the user experience you want to provide. - For sensitive information like passwords, it's a good practice to set autocomplete to off to prevent the browser from saving or suggesting such sensitive data.
- Be aware that some browsers may not fully support the
autocomplete
attribute, so always test your forms across different browsers.
🎉 Conclusion
The autocomplete
attribute is a valuable tool for controlling the autocomplete behavior of form elements in HTML. By understanding and using this attribute appropriately, you can enhance the usability and security of your web forms.
👨💻 Join our Community:
Author
For over eight years, I worked as a full-stack web developer. Now, I have chosen my profession as a full-time blogger at codetofun.com.
Buy me a coffee to make codetofun.com free for everyone.
Buy me a Coffee
If you have any doubts regarding this article (HTML autocomplete Attribute), please comment here. I will help you immediately.