function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
sdbsdb 

Enable or disable an apex commandButton depending on whether a checkbox is checked or unchecked

What I'm trying to do is dynamically enable and disable an apex commandButton depending on whether a checkbox is checked or unchecked. The commandButton is starting in a disabled state, and if I check the checkbox I want to enable the commandButton. What happens is that the commandButton does in fact become enabled (if I click it, the form is submitted), but visually the commandButton still appears to be disabled. However if I do the same thing with a regular input button, everything works fine -- ie the input button appears enabled when enabled and disabled when disabled.  Below is a very simple VF page that demonstrates the problem.

 

<apex:page sidebar="false"> 

<apex:form >   

<apex:pageBlock >     

<apex:pageBlockButtons id="buttonBlock" location="top">       

<apex:commandButton id="myCmdButton" value="Disabled Apex Command Button" disabled="true"/>       

<input id="myButton" type="button" value="Disabled Input Button" disabled="true"></input>       

<input id="myCheckbox" type="checkbox" onclick="cbxOnClick()">Enable Buttons</input>       

<script>           

var cmdBtn = document.getElementById('{!$Component.myCmdButton}');           

var btn = document.getElementById('myButton');           

var cbx = document.getElementById('myCheckbox');                                     

function cbxOnClick() {           

btn.disabled=!cbx.checked;               

btn.value=(cbx.checked? "Enabled Input Button":"Disabled Input Button");               

cmdBtn.disabled=!cbx.checked;               

cmdBtn.value=(cbx.checked? "Enabled Apex Command Button":"Disabled Apex Command Button");           

}                                                                                                 

</script>     

</apex:pageBlockButtons>   

</apex:pageBlock>         

</apex:form>       

</apex:page>

Best Answer chosen by Admin (Salesforce Developers) 
Starz26Starz26

You could render your pageblock based on the value of the chkbox.

 

Then you could add another pageblocktable which contains .img file of a button that appears disabled, or just display text on what the user needs to do and have it rendered based on the chkbox value.

 

When the chkbox value changes rerender those two pageblocks and you will be able to simulate it.

 

The only other way is to display a message to the user that informs them of what they need to do if the button is disabled and is clicked.

All Answers

Starz26Starz26

You could render your pageblock based on the value of the chkbox.

 

Then you could add another pageblocktable which contains .img file of a button that appears disabled, or just display text on what the user needs to do and have it rendered based on the chkbox value.

 

When the chkbox value changes rerender those two pageblocks and you will be able to simulate it.

 

The only other way is to display a message to the user that informs them of what they need to do if the button is disabled and is clicked.

This was selected as the best answer
sdbsdb

Thanks, I appreciate the answer. i was mainly wondering if this was a bug or expected behavior, and it sounds like it's expected behavior. I am able to get this to work by binding the 'disabled' attribute of the commandButton to a property on the controller, and then using an actionFunction to rerender the button when the user clicks the checkbox. Thanks again.

NahuelNahuel

Hello there!, 

 

Can you paste the fragment of code that you mention in your last post? Im trying to achieve this same result but to no avail, tried everything, javascript, controller variable bindings, everything, but I cant seem to make this work :( 

 

Thank you in advance for any help you can give me!