Edit: I just noticed Chrome and Firefox already achieve with readonly
what I’m looking for: noneditable, selectable, and greyed out. The issue is that on the Pale Moon browser, the readonly
inputs are not greyed out.
I’m keeping the question open for now, in case somebody has insight to provide.
Edit 2: Nope, also in Chrome and Firefox, the readonly
inputs are not greyed out. I got confused because on the MDN page, there is a site CSS that greys out the readonly
inputs… (using the :read-only
pseudo-class, which is not supported on Pale Moon)
Original question:
I have <input type="text">
and <textarea>
elements, whose content should not be editable at some steps.
So, initially I added a disabled
property to these elements. The elements become noneditable, and greyed out. This behavior is fine.
However, their content should be selectable, and the disabled
property avoids that. So I switched to the readonly
property. The elements are still noneditable, and the content can be selected, but they are no longer greyed out.
So, I’m looking for a solution to have elements that are: noneditable, selectable, and greyed out.
- I could apply some CSS, but I would prefer to have the native browser styles of disabled inputs.
- The elements are not part of a
<form>
, so there is no need to worry about form submissions.
2
Answers
To achieve the desired behavior of having noneditable, selectable, and greyed-out elements without using the disabled property, you can use a combination of CSS and the readonly attribute. You can style the readonly elements to look like they are disabled while preserving the ability to select their content. Here’s an example of how you can achieve this:
Unfortunately, the native styles of disabled inputs may vary across browsers, and there’s no direct way to achieve the exact appearance while preserving content selection.
However, you can use a combination of styles to come close to the disabled input appearance while maintaining the content selectability
There are multiple ways to achieve this:
1. You can just leave both attributes within your element, since you are looking for an HTML-only solution. For example:
Whenever you need the element enabled, you can remove the attributes, using JavaScript, or set the attributes if you want to disable the element:
Enabling the element
Disabling the element
2. Alternatively, you can try leaving the
readonly
property, so that the contents cannot be edited and can be selected, and create a CSS rule for a custom class selector and play with the opacity of the element. For example:Since you mentioned that you only need the
readonly
attribute for some steps of the entire flow, you can toggle the class into or out of the element each time you need it to. For example:If the element already contains the toggled class, then the class will be removed and vice versa. If you need the class removed, so that the element appears active again, you can proceed to remove the element’s attribute as well:
3. You could also skip the
readonly
anddisabled
attributes entirely and create a CSS rule that groups all your requirements together. For example:Similarly, every time you need to enable the element you can just toggle the class out of the element, and vice versa, like before:
Anything suggested above achieves what you are looking for.