Distinguishing User Interaction with `disabled` vs. `readonly` Fields

2024-07-27

  • Disables the input field entirely. Users cannot interact with it by typing, selecting, or clicking.
  • The field's value is not submitted with the form.
  • Browsers typically render disabled fields with a grayed-out appearance, indicating they're inactive.

Use Cases for disabled:

  • Fields that depend on other input (e.g., a confirmation field disabled until a password is entered).
  • Pre-filled information that shouldn't be changed (e.g., order confirmation details).
  • Fields temporarily unavailable (e.g., during form validation).

readonly Attribute:

  • Makes the input field read-only. Users can see the value but cannot edit it directly.
  • Browsers may display read-only fields with a slightly different style (e.g., lighter background) to suggest they're uneditable.

Use Cases for readonly:

  • Displaying pre-filled data that might be referenced later (e.g., username).
  • Showcasing calculated values (e.g., order total).
  • Providing contextual information that users don't need to modify (e.g., system-generated ID).

Cross-Browser Compatibility:

Both disabled and readonly attributes are widely supported across all major browsers (Chrome, Firefox, Safari, Edge, etc.). Their behavior is consistent, so you don't need to worry about browser-specific differences.

Key Differences Summary:

AttributeInteractionSubmitted with Form
disabledCompletely disabledNo
readonlyRead-only, can't editYes

Choosing the Right Attribute:

  • Use disabled when you want to prevent user interaction and exclude the field's value from form submission.
  • Use readonly when you want to display pre-filled or calculated data that should be submitted with the form.



<form>
  <label for="name">Name:</label>
  <input type="text" id="name" name="name" value="John Doe" disabled>

  <label for="confirmation">Confirm Password:</label>
  <input type="password" id="confirmation" name="confirmation" disabled>

  <button type="submit">Submit</button>
</form>
  • In this example, the name field is pre-filled with "John Doe" and disabled. Users cannot edit it.
  • The confirmation field is initially disabled, but it might become enabled with JavaScript code after a password is entered in another field (not shown here).

Readonly Field:

<form>
  <label for="username">Username:</label>
  <input type="text" id="username" name="username" value="user123" readonly>

  <label for="orderTotal">Order Total:</label>
  <input type="number" id="orderTotal" name="orderTotal" value="100.50" readonly>

  <button type="submit">Submit</button>
</form>
  • The username field displays a pre-filled username ("user123") and is read-only. Its value will be submitted with the form.
  • The orderTotal field shows the calculated order total (100.50) as read-only and will also be submitted with the form.



  • Use JavaScript to dynamically enable or disable input fields based on user actions or form validation. For example, enabling a confirmation field only after a password is entered.
<form>
  <label for="password">Password:</label>
  <input type="password" id="password" name="password">

  <label for="confirmPassword">Confirm Password:</label>
  <input type="password" id="confirmPassword" name="confirmPassword" disabled>

  <button type="submit">Submit</button>
</form>

<script>
  const passwordInput = document.getElementById('password');
  const confirmPasswordInput = document.getElementById('confirmPassword');

  passwordInput.addEventListener('input', () => {
    confirmPasswordInput.disabled = passwordInput.value === ''; // Enable if password is entered
  });
</script>

Hidden Fields for Pre-filled Data:

  • If you have pre-filled data that needs to be submitted but shouldn't be editable, consider using a hidden field along with a read-only field for display. This ensures the data is included in the form submission.
<form>
  <label for="username">Username:</label>
  <input type="text" id="usernameDisplay" name="usernameDisplay" value="user123" readonly>

  <input type="hidden" id="username" name="username" value="user123">  <button type="submit">Submit</button>
</form>

Custom CSS Styling:

  • You can use CSS to style read-only fields to visually differentiate them from regular input fields. This provides a user-friendly way to indicate that the field cannot be edited.
<form>
  <label for="orderTotal">Order Total:</label>
  <input type="number" id="orderTotal" name="orderTotal" value="100.50" readonly>
</form>

<style>
  #orderTotal {
    background-color: #eee;
    border: 1px solid #ddd;
  }
</style>
  • For basic control over user interaction, disabled and readonly attributes are usually sufficient.
  • When you need dynamic behavior based on user actions or validation, JavaScript is a valuable option.
  • If you require both display and submission of pre-filled data, combine a read-only field with a hidden field.
  • Use CSS styling to enhance the user experience by visually indicating read-only fields.

html cross-browser



Ensuring a Smooth User Experience: Best Practices for Popups in JavaScript

Browsers have built-in popup blockers to prevent annoying ads or malicious windows from automatically opening.This can conflict with legitimate popups your website might use...


Why You Should Use the HTML5 Doctype in Your HTML

Standards Mode: The doctype helps the browser render the page in "standards mode" which ensures it follows the latest HTML specifications...


Cross-Browser Rounded Corners Made Easy: Mastering the border-radius Property in CSS

In CSS (Cascading Style Sheets), the border-radius property allows you to add a curved effect to the corners of an element's outer border...


Enhancing Textarea Usability: The Art of Auto-sizing

We'll create a container element, typically a <div>, to hold the actual <textarea> element and another hidden <div>. This hidden element will be used to mirror the content of the textarea...


Example Codes for Customizing Numbering in HTML Ordered Lists

In HTML, ordered lists are created using the <ol> tag.Each item within the list is defined using the <li> tag.By default...



html cross browser

Fixing Width Collapse in Percentage-Width Child Elements with Absolutely Positioned Parents in Internet Explorer 7

In IE7, when you set a child element's width as a percentage (%) within an absolutely positioned parent that doesn't have an explicitly defined width


Unveiling the Mystery: How Websites Determine Your Timezone (HTML, Javascript, Timezone)

JavaScript Takes Over: Javascript running in the browser can access this information. There are two main methods:JavaScript Takes Over: Javascript running in the browser can access this information


Unleash the Power of Choice: Multiple Submit Button Techniques for HTML Forms

An HTML form is a section of a webpage that lets users enter information. It consists of various elements like text boxes


Unveiling Website Fonts: Techniques for Developers and Designers

The most reliable method is using your browser's developer tools. Here's a general process (specific keys might differ slightly):


Alternative Methods for Disabling Browser Autocomplete

Understanding AutocompleteBrowser autocomplete is a feature that helps users quickly fill out forms by suggesting previously entered values