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
Ap30Ap30 

Command button is not getting disabled

Hi All,
Command btton is not getting disabled. Condition is written in javascript.

============
<apex:page controller="vRegistration" id="page">

<script>
    function getValidation(){
      
        var at = document.getElementById('{!$Component.page.form.pb.pbs.amt}');
        if(at.value == "")
        {
           document.getElementById('{!$Component.page.form.pb.pbs.email}').disabled = true;
        }
        else
        {
            return true;
        }
     }
</script>

  <apex:form id="form">
  <apex:actionFunction action="{!sendEmail}" name="sendEmail" reRender="pb" />
  <apex:actionFunction action="{!generatePDF}" name="generatePdf" reRender="pb"/>
  <apex:pageBlock title="Registration Form" id="pb">
  <apex:pageBlockSection columns="2" id="pbs">
  Company Name :<apex:inputText value="{!vcname}" id="name"/>
  Amount :<apex:inputText value="{!apevent}" id="amt"/>
    </apex:pageBlockSection>
  <apex:outputPanel >
  <apex:commandButton value="Send Email" id="email" action="{!sendEmail}" onclick="getValidation();"/>
  <apex:commandButton value="Generate PDF" action="/apex/generatePDFpage"  onclick="return generatePDF()" />
  </apex:outputPanel>
  
  
  </apex:pageBlock>
  
  </apex:form>
</apex:page>
ANUTEJANUTEJ (Salesforce Developers) 
Hi there,

>> https://webkul.com/blog/using-condition-visualforce/

The above link has an implementation that conditionally greys out the command button you can try checking this and modify it to fit your use-case.

Let me know if it helps you and close your query by marking it as solved so that it can help others in the future.  

Thanks.
Ap30Ap30
Thanks for the reply. But i have to implement this using Javascript.
Suraj Tripathi 47Suraj Tripathi 47
Hi Ap30,
Greetings!

You are not using the correct hierarchy.
Use like this.
<apex:page controller="vRegistration" id="page">

<script>
    function getValidation(){
      
        var at = document.getElementById('{!$Component.page.form.pb.pbs.amt}');
        if(at.value == "")
        {
           document.getElementById('{!$Component.page.form.pb.op.email}').disabled = true;
        }
        else
        {
            return true;
        }
     }
</script>

  <apex:form id="form">
      <apex:actionFunction action="{!sendEmail}" name="sendEmail" reRender="pb" />
      <apex:actionFunction action="{!generatePDF}" name="generatePdf" reRender="pb"/>
      <apex:pageBlock title="Registration Form" id="pb">
          <apex:pageBlockSection columns="2" id="pbs">
              Company Name :<apex:inputText value="{!vcname}" id="name"/>
              Amount :<apex:inputText value="{!apevent}" id="amt"/>
            </apex:pageBlockSection>
          <apex:outputPanel id="op">
              <apex:commandButton value="Send Email" id="email" action="{!sendEmail}" onclick="getValidation();"/>
              <apex:commandButton value="Generate PDF" action="/apex/generatePDFpage"  onclick="return generatePDF()" />
          </apex:outputPanel>
    </apex:pageBlock>
  </apex:form>
</apex:page>
Thank you!
Regards,
Suraj Tripathi
Ap30Ap30
I changed my code. Still i'm able to send email when amount is not entered.
Maharajan CMaharajan C
Hi,

Try the below code:

change the command busston as lie below and call the action function name from JS: 
<apex:commandButton value="Send Email" id="email" onclick="getValidation();return false;"/>  // this will stop the button submission.
 
<apex:page controller="vRegistration" id="page">

<script>
    function getValidation(){
      
        var at = document.getElementById('{!$Component.page.form.pb.pbs.amt}');
        if(at.value == "")
        {
		   document.getElementById('{!$Component.page.form.pb.pbs.op.email}').disabled = true;
        }
        else
        {
            sendEmail();
        }
     }
</script>

  <apex:form id="form">
  <apex:actionFunction action="{!sendEmail}" name="sendEmail" reRender="pb" />
  
  <apex:actionFunction action="{!generatePDF}" name="generatePdf" reRender="pb"/>
  <apex:pageBlock title="Registration Form" id="pb">
  <apex:pageBlockSection columns="2" id="pbs">
  Company Name :<apex:inputText value="{!vcname}" id="name"/>
  Amount :<apex:inputText value="{!apevent}" id="amt"/>
    </apex:pageBlockSection>
  <apex:outputPanel id="op">
  <apex:commandButton value="Send Email" id="email" onclick="getValidation();return false;"/>
  <apex:commandButton value="Generate PDF" action="/apex/generatePDFpage"  onclick="return generatePDF()" />
  </apex:outputPanel>
  
  
  </apex:pageBlock>
  
  </apex:form>
</apex:page>

Thanks,
Maharajan.C
Ap30Ap30
Still its not working.