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
Melissa TomaskoMelissa Tomasko 

How can a disable a button

Hi Guys,

I have created a custom button that when clicked will trigger an email that will allow my sales team members to request and account.

My issue is that I would like to have this button, "Request Account" disabled after the email is sent so that there are not multiple requests for the same account. I would also like the button to reappear after I have made the transfer.

What is the best way to do this?

karthikeyan perumalkarthikeyan perumal
Hello, 

Try below code with your button:  2 ways you can do this: 

Using Javascript : 
 
<script src="//ajax.googleapis.com/ajax/libs/jquery/latest/jquery.js"></script>
<script>

    function buttonsEnabled(enabled) {
        // retrieve all of the buttons or links on the page
        // with the css class of btn
        var $buttons = jQuery('.btn');
        if (enabled === false) {
            // add the btnDisabled class to give it the look of being disabled
            // add the disabled attribute to actually disable interactability
            $buttons.toggleClass('btnDisabled', true).attr('disabled', 'disabled');
        } else {
            // remove the css class and the disabled attribute
            $buttons.toggleClass('btnDisabled', false).attr('disabled', null);
        }
    }

    function doSomeWork() {
        // first, call the action function to post the form
        doSomeWorkActionFunction();

        // second, disable the buttons
        buttonsEnabled(false);

        // third, return false to prevent the click from
        // posting the form a second time
        return false;
    }

</script>
<apex: page>
<apex:form>

    <apex:actionFunction name="doSomeWorkActionFunction"
        action="{!yourControllerMethod}"
        oncomplete="buttonsEnabled(true);"
        rerender="whateverYouNeedToRerender"></apex:actionFunction>

    <apex:commandLink action="{!yourControllerMethod}"
        value="Your Text Here"
        id="theCommandLink"
        onclick="return doSomeWork();" />

</apex:form>
<apex:page>


Using Controller: 

Class sample code: 
 
public Boolean spehereEnabled = false;    

public void enableSpehere()
  {
    spehereEnabled = true;
  }

Button in VF page: 
 
<apex:commandButton disabled="{!spehereEnabled}" value="{!enableSpehere}" value="Enable Spehere" />

Note that you had both your method named enableSpehere and also your Boolean property. I changed the Boolean property name for you to spehereEnabled. You'd just change the pageBlockSection rendered binding to...
<apex:pageBlockSection rendered="{!sphereEnabled}"\>


Hope this will help you. 

Thanks
karthik