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 readonly Attribute
Photo Credit to CodeToFun
🙋 Introduction
The readonly
attribute is a valuable feature in HTML that allows developers to designate form elements as read-only.
When applied, users can view the content but cannot modify or interact with it.
This attribute is particularly useful when you want to display information that should not be altered by the user.
🎯 Purpose of readonly
The primary purpose of the readonly
attribute is to restrict user input for specific form elements.
Unlike the disabled attribute, which prevents any interaction, the readonly
attribute allows users to view the content while preventing them from making changes.
💎 Values
The readonly
attribute has a boolean value, and it can be set to either:
- readonly: This value indicates that the form element is read-only.
- unset: This value removes the readonly status, allowing the user to interact with the element.
📄 Example
Let's look at a simple example of how to use the readonly
attribute in an HTML form:
<form>
<label for="username">Username:</label>
<input type="text" id="username" name="username" readonly>
<label for="email">Email:</label>
<input type="email" id="email" name="email" readonly="readonly">
<textarea id="comments" name="comments" readonly>This is a read-only textarea.</textarea>
<input type="submit" value="Submit">
</form>
🧠 How it Works
In this example, the readonly
attribute is applied to the username input field, the email input field, and a textarea. Users can view the information in these fields but cannot make any modifications.
🔄 Dynamic Values with JavaScript
The readonly
attribute can also be manipulated dynamically using JavaScript. Here's a quick example:
<script>
// Dynamically set readonly for an input field
document.getElementById("dynamicField").readOnly = true;
</script>
🧠 How it Works
In this script, the readonly
attribute is set to true for an input field with the id dynamicField. This kind of dynamic control can be beneficial when you need to adjust the read-only status based on certain conditions or user actions.
🏆 Best Practices
- Use the
readonly
attribute when you want to display information that should not be altered by the user. - Be cautious with using readonly for sensitive information, as it only prevents accidental changes and can be easily bypassed by users with some technical knowledge.
- Always test your forms to ensure compatibility across different browsers.
🎉 Conclusion
The readonly
attribute is a useful tool for controlling user interaction with form elements in HTML.
By incorporating this attribute appropriately, you can create more user-friendly and secure 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 readonly Attribute), please comment here. I will help you immediately.