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
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.

thanks

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

<apex:page controller="EnableButtonController">
    <script>
    function test()
    {
      console.log('test>>>');
      methodOneInJavascript(); 
    }

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

Controller class:

public class EnableButtonController {
    public boolean chkBx{get;set;}
    public Boolean show{get;set;}
    public EnableButtonController()
    {
        chkBx =false;
        show=true;
    }
    
  public void methodOne()
  {
      if(chkBx== true)
      System.debug(':::::');
      show =false;
  }
   public PageReference showPage() {
      
        PageReference blankPage = new PageReference('/apex/BlankVf?');//Your vf page that should load after clicking the button
        blankPage.setRedirect(true);
        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
Debaranjan GhoshDebaranjan Ghosh
Hello Deepali

I tested this and it satisfied both the Conditions 

1.when page loads command button should be disabled.

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

So I think this can be marked as the Best Answer so far as No other solution was proposed.
But Shweta should confirm

Debaranjan