<apex:commandButton value="Save" rerender="IdPanel" 
disabled="{!AND(SelectedText == null && SelectedText == '')}" />
    <apex:outputPanel id="IdPanel">
      <apex:repeat var="TheID" value="{!CurrentList}"> 
    <apex:outputText value="{!TheID}" />;

How can I enable/disable commandButton based on the user have selected?

Here I'm checking to see if the User have selectedText and if the SelectedText is null or empty I want the commandButton to be disabled but the below code does disable the button but but never enable even though I have value in the SelectedText

<apex:commandButton value="Save" rerender="IdPanel" 
disabled="{!AND(SelectedText == null && SelectedText == '')}" />
  • Are you rerendering the container (e.g. Apex:OutputPanel) that the commandButton is in?
    – Dan Jones
    Aug 10 '15 at 14:45
  • I have updated my question please have a look
    – Nick Kahn
    Aug 10 '15 at 14:47
  • 3
    How can SelectedText be both null and an empty string at the same time? Would an OR not be better?
    – BarCotter
    Aug 10 '15 at 14:50
  • yes make no diff if I replace with || (OR)
    – Nick Kahn
    Aug 10 '15 at 14:51
  • Can you add more detail to what you want to happen? Do you want the button to be disabled as soon as they fill in the SelectedText text? or is this something that happens after they click Save?
    – BarCotter
    Aug 10 '15 at 15:00

Without knowing much about your code, the following works assuming you have something like...

public String selectedText {

... In your controller.

See below:

    <apex:actionFunction name="myActFunct" reRender="myPanel" />
    <apex:inputText value="{!selectedText}" onkeyup="myActFunct();" />
    <apex:outputPanel id="myPanel">
        <apex:commandButton value="Save" rerender="IdPanel" disabled="{!AND(SelectedText == null || SelectedText == '')}" />

Basically you've got your inputText which, onkeyup, calls the ActionFunction which then rerenders the panel and then can check whether or not SelectedText is empty or not.

I've tested this and it should meet your requirements.

Good luck!

  • 1
    oops.. didnt see your answer before posting mine. Slight change in mine that instead of actionFunction, I used actionSupport. :-)
    – AslamK
    Aug 10 '15 at 15:24
  • No problem! Both ways work which is the important thing. :)
    – Dan Jones
    Aug 10 '15 at 15:34

Its quite simple to achieve this. You just need to capture the keyup action and rerender the section containing the CommandButton. Use ActionSupport to capture the events as shown below.

Also added an actionStatus so that you know that the re-rendering is happening.

  <apex:pageBlock id="someblock">
        <apex:inputText value="{!SelectedText}">
            <apex:actionSupport event="onkeyup" reRender="commandButtonPanel" status="status"/>

        <apex:outputPanel id="commandButtonPanel">
            <apex:commandButton action="{!enable}" disabled="{!SelectedText==null}" value="Submit"/>

        <apex:actionStatus startText="Re-evalutaing..."  stopText="Idle" id="status"/>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.