2

I have the following piece of code

<td width="10%"/>
<td width="80%"><input type="checkbox" name="checkbox-mini-1" id="checkbox-mini-1" data-mini="true" onclick="javascript: toggleButton(this,'j_id0:LandingForm:ApproveButton');">
</input>&nbsp;<b>Yes, I accept</b></td>
<td></td>

<apex:commandButton id="ApproveButton" value="Register and Accept Invitation" style="display:none;width:125px;height:25px;font-weight:bold;font-size:12px;background-color:#f9f9f9;border-radius:6px;" action="{!loginAndAccept}"/>&nbsp;&nbsp;
<apex:commandButton id="LoginButton" value="Login and Accept Invitation" style="display:none;width:125px;height:25px;font-weight:bold;font-size:12px;background-color:#f9f9f9;border-radius:6px;" action="{!registerAndAccept}"/>&nbsp;&nbsp;

I want to display one of the two buttons above depending on the value of a variable hasuser which is present in the controller. There should be 2 conditions-1.checkbox clicked and value of hasuser is false, then first button gets displayed. 2. checkbox clicked and value of user is true, then the second button gets displayed . I am not being able to apply both the conditions simultaneously in the onclick. Fairly new to salesforce. Any help would be appreciated

2 Answers 2

1

Command Button in salesforce comes with an attribute rendered .You will have to make use of same .

Also to write conditions inside render tag you can use FORMULAE

You can call an action function onclick of the checkbox and process the logic inside your actionfunction and return the boolean and use the boolean on the rendered attribute of the command button .

PSEUDO CODE

<apex:commandButton id="ApproveButton" value="Register and Accept Invitation" action="{!loginAndAccept}" rendered="{!registerBoolean}"/>

Apex:

public boolean registerBoolean {get;set;}
constructor(){
  registerBoolean = true;
}
public pagereference loginAndAccept(){
  registerBoolean = false;
  return true;
}
0

Try this code using RemoteAction :

VF page :

<apex:inputCheckbox onchange="changeState(this)" rerender="buttonsDiv"/>

<div id="buttonsDiv">
    <apex:commandButton id="ApproveButton" value="Register and Accept Invitation" action="{!loginAndAccept}" rendered="{!hasUser== 'false'}"/>
    <apex:commandButton id="LoginButton" value="Login and Accept Invitation" action="{!registerAndAccept}" rendered="{!hasUser == 'true'}"/>
</div>

<script type="text/javascript">    
    function changeState(state) {
        var isChecked = (state.checked) ? 'false' : 'true';

        Visualforce.remoting.Manager.invokeAction(
            '{!$RemoteAction.YourController.changeChkState}',
            isChecked,
            function (result, event) {
            }, 
            {escape: false}
        );
    }
</script>

Controller :

public class YourController {
    public String hasUser {get; set;}

    @RemoteAction
    public void changeChkState(state) {
       hasUser = state;
    }
}

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.