[SOLVED] How do I verify whether a readonly attribute was defined for an input element using Javascript | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3

[SOLVED] How do I verify whether a readonly attribute was defined for an input element using Javascript

Assuming an input element as follows <input type="number" id="num" readonly value="1000" /> How do I verify whether 'readonly' was defined (or not) for that element? In JS code section I tried console.log(<element-id>.readonly); // empty Also I tried console.log(<element-id>.getAttribute("readonly"); But I'm not sure whether any of these actually tell me whether or not 'readonly' attribute was defined, because they give different result. I just need to be confirmed whether 'readonly' attribute was defined in case I want to remove, toggle or define the attribute (depending on previous status). And I feel that removing, toggling or defining an attribute makes no sense when its previous presence is unknown in the first place anyways. Thanks in advance 🙏

2nd Aug 2020, 11:32 PM
Ipang
2 Answers
+ 2
Close, it's `myinput.readOnly` :) It's a boolean and you can just set a new value to update the input on the page.
2nd Aug 2020, 11:37 PM
Schindlabua
Schindlabua - avatar
+ 2
Schindlabua Thanks a bunch 🙏 The getAttribute() still not giving me a good hint even passing 'readOnly' as argument. I guess I'll take the <element>.readOnly approach 😂
3rd Aug 2020, 12:25 AM
Ipang