I would like an input to be conditionally read-only, but I cannot find a way to make a boolean HTML attribute like html-readonly conditional within VisualForce. I am avoiding Javascript to remove the attribute mainly because I am new to VisualForce and want to see what I can do with pure VisualForce (minimal JS).

I can't use:

<apex:inputText html-readonly="{!makeItReadOnly}" value="{!foo}"/>

Because browsers evaluate any value (including false) for a boolean attribute as true.

My current work-around isn't particularly clean (though accomplishes my goal):

<apex:inputText rendered="{!makeItReadOnly}" value="{!foo}"/>
<apex:inputText html-readonly="true" rendered="{!!makeItReadOnly}" value="{foo}"/>

For normal apex:inputText fields, use the disabled attribute:

<apex:inputText disabled="{!makeItReadOnly}" value="{!foo}"/>

Your Answer

