+ Start a Discussion
shweta kumari 25shweta kumari 25 

enable button on checkbox click in visualforce page

I have a checkbox and a command button inside a visualforce page.

when page loads command button should be disabled.

Only when I check the checkbox then the command buton should be enabled and on click it will navigate to another page.

Please anyone post a working code for the scenario.


Deepali KulshresthaDeepali Kulshrestha
Hi Shweta,
Please try this simple code to get the expected results.
Vf page:

<apex:page controller="EnableButtonController">
    function test()

    <apex:form id="frm">
  <apex:actionFunction action="{!methodOne}" name="methodOneInJavascript" reRender="frm"/>
    Checkbox: <apex:inputcheckbox value="{!chkBx}" onchange="test();">
      </apex:inputcheckbox> <br/><br/>
      <apex:commandButton value="Button" disabled="{!show}" action="{!showPage}"/>

Controller class:

public class EnableButtonController {
    public boolean chkBx{get;set;}
    public Boolean show{get;set;}
    public EnableButtonController()
        chkBx =false;
  public void methodOne()
      if(chkBx== true)
      show =false;
   public PageReference showPage() {
        PageReference blankPage = new PageReference('/apex/BlankVf?');//Your vf page that should load after clicking the button
        return blankPage;

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Deepali Kulshrestha