You have numerous code issues in your question. The very first example will work if you got the property names correctly:
document.getElementById("txtbox").readOnly = true;
Note the uppercase letters ( getElementById instead of getElementById and readOnly instead of readOnly ).
Here is a working example .
As for your second attempt, the disabled property, not disable . And your 3rd attempt, the setAttribute method, not setAttribute . As you can see, JavaScript is case sensitive!
source share