Make form controls - even
<select>
- read-only.
Readonly.js is a lightweight wrapper to fix the inconsistency of the readonly
attribute in form controls. According to current specifications the attribute will be ignored in certain input types, and is completely void in <select>
elements. This little helper aims to fix that. If you're curious to know more read the Web Standards section.
If you're coming from older versions take note:
select[readonly] { ... }
change
event on the element. .e.g input.dispatchEvent(new Event('change'))
. This will make sure that changes to name
and/or value
made via JavaScript will take effect.Version 3.0.0 was rewritten with a modern syntax but we're using Babel to transpile it into a more compatible JavaScript syntax wise. We also make use of some relatively newer browser APIs like querySelectorAll
. Anything newer than IE11 should be fine but if you run into issues please submit an issue so we can look into it.
Only a single function is exported: readonly
. e.g.
readonly('input, select');
This will toggle the read-only state of matching elements.
The first argument can be either:
You can also force a state by passing an additional argument; true
for read-only or false
for editable.
readonly('input, select', true);
If you're using jQuery it'll be installed as a plugin.
$('input, select').readonly(true);
Have you ever wondered why the readonly
attribute doesn't work in <select>
elements? Well, the whole thing is more complicated then one might think, but you can read all about it here: https://github.com/whatwg/html/issues/2311. You also could help advance the web standards by leaving a comment with why such feature would be useful to you.
The MIT License © 2013 Arthur Corenzan. See LICENSE.md for full notice.