The <apex: inputCheckbox> is a HTML input element of type checkbox. It is another input element used for getting the input from the user for the controller method that doesn't correspond to the field on a Salesforce object.
The HTML pass-through attributes are supported by this component using the "html-" prefix, which are attached to the generated <input> tag.
The <apex: inputCheckbox> has the following attributes:
1. accesskey
The acesskey is a string type attribute that defines the keyboard access key that puts the checkbox in focus. We can select or deselect the checkbox when it is in focus.
Syntax
<apex:inputCheckbox accesskey="f"></apex:inputCheckbox>
2. dir
The dir is a string-type attribute used to specify the direction in which the generated HTML component should be used. RTL and LTR are the two possible values for the dir attribute.
Syntax
<apex:inputCheckbox dir="RTL"></apex:inputCheckbox>
3. disabled
The disabled is a Boolean type attribute that specifies whether this checkbox should be displayed in the disabled state or not. By default, its value is set to false.
Syntax
<apex:inputCheckbox disabled="true"></apex:inputCheckbox>
4. id
The id is a string type attribute, i.e., a unique identifier that allows this component to be referenced by other components in the page.
Syntax
<apex:inputCheckbox id="checkboxId"></apex:inputCheckbox>
5. immediate
The immediate attribute is of type Boolean. If its value is true, the action associated with the component will happen immediately. The function will not process the validation rules associated with the checkbox on the page.
Syntax
<apex:inputCheckbox immediate="true"></apex:inputCheckbox>
6. label
The label is a string-type attribute used to display text next to the control and reference the control in the error message.
Syntax
<apex:inputCheckbox label="Label Text"></apex:inputCheckbox>
7. lang
The lang is a string-type attribute used for specifying the base language used for the generated HTML output. The possible values of this attribute are en and en-US.
Syntax
AD
<apex:inputCheckbox lang="en"></apex:inputCheckbox>
8. onblur
The onblur is a string-type attribute used to invoke the JavaScript method when the onblur event occurs or simply when the focus moves off the checkbox field.
Syntax:
<apex:inputCheckbox onblur="saveRecords();"></apex:inputCheckbox >
9. onchange
The onchange is a string-type attribute used to invoke the JavaScript method when the onchange event occurs or simply when the user changes the content of the checkbox field.
Syntax:
<apex:inputCheckbox onchange="saveRecords();"></apex:inputCheckbox>
10. onclick
The onclick is a string-type attribute used to invoke the JavaScript method when the onclick event occurs or when the user clicks the checkbox field.
AD
Syntax:
<apex:inputCheckbox onclick="saveRecords();"></apex:inputCheckbox>
11. ondblclick
The ondblclick is a string type attribute used to invoke the JavaScript method when the ondblclick event occurs or when the user clicks the checkbox field twice.
Syntax:
<apex:inputCheckbox ondblclick="saveRecords();"></apex:inputCheckbox>
12. onfocus
The onfocus is a string-type attribute used to invoke the JavaScript method when the input event occurs or the focus is on the field.
Syntax:
<apex:inputCheckbox onfocus="saveRecords();"></apex:inputCheckbox>
13. onkeydown
The onkeydown is a string-type attribute used to invoke the JavaScript method when the onkeydown event occurs or simply when the user presses a keyboard key.
Syntax:
<apex:inputCheckbox onkeydown="saveRecords();"></apex:inputCheckbox>
14. onkeypress
The onkeypress is a string-type attribute used to invoke the JavaScript method when the onkeypress event occurs or simply when the user holds down or presses a keyboard key.
Syntax:
<apex:inputCheckbox onkeypress="saveRecords();"></apex:inputCheckbox>
15. onkeyup
The onkeyup is a string-type attribute used to invoke the JavaScript method when the onkeyup event occurs or when the user releases a keyboard key.
Syntax:
<apex:inputCheckbox onkeyup="saveRecords();"></apex:inputCheckbox>
16. onmousedown
The onmousedown is a string-type attribute used to invoke the JavaScript method when the onmousedown event occurs or simply when the user presses the mouse button.
Syntax:
<apex:inputCheckbox onmousedown="countKeys();"></apex:inputCheckbox>
17. onmousemove
The onmousemove is a string type attribute used to invoke the JavaScript method when the onmousemove event occurs or simply when the user moves the mouse pointer.
Syntax:
<apex:inputCheckbox onmousemove="countKeys();"></apex:inputCheckbox>
18. onmouseout
The onmouseout is a string-type attribute used to invoke the JavaScript method when the onmouseout event occurs or simply when the user moves the mouse pointer away from the checkbox.
Syntax:
<apex:inputCheckbox onmouseout="countKeys();"></apex:inputCheckbox>
19. onmouseover
The onmouseover is a string-type attribute used to invoke the JavaScript method when the onmouseover event occurs or simply when the user moves the pointer over the <apex: inputCheckbox>.
Syntax:
<apex:inputCheckbox onmouseover="countKeys();"></apex:inputCheckbox>
20. onmouseup
The onmouseup is a string-type attribute used to invoke the JavaScript method when the onmouseup event occurs or when the user releases the mouse button.
Syntax:
<apex:inputCheckbox onmouseup="countKeys();"></apex:inputCheckbox>
21. onselect
The onselect is a string-type attribute used to invoke the JavaScript method when the onmouseup event occurs or simply when the user selects the checkbox.
Syntax:
<apex:inputCheckbox onselect="countKeys();"></apex:inputCheckbox>
22. rendered
The rendered is a Boolean type attribute that specifies whether this component needs to be rendered on the page or not. By default, its value is set to true.
Syntax:
<apex:inputCheckbox rendered="countKeys();"></apex:inputCheckbox>
23. required
The required is a Boolean type attribute that specifies whether this checkbox is a required field or not. By default, its value is set to false.
Syntax:
<apex:inputCheckbox required="true"></apex:inputCheckbox>
24. selected
The selected is a Boolean type attribute that specifies whether this checkbox should be rendered in its checked state or not. By default, its value is set to false.
Syntax:
<apex:inputCheckbox selected="true"></apex:inputCheckbox>
25. style
The style is a string type attribute used to specify the inline CSS style that will be applied for displaying the checkbox component.
Syntax:
<apex:inputCheckbox style="display:none;"></apex:inputCheckbox>
26. styleClass
The styleClass is a string type attribute used to specify the style class that will be applied for displaying the checkbox component.
Syntax:
<apex:inputCheckbox styleClass="checkboxClass"></apex:inputCheckbox>
27. tabindex
The tabindex is a string type attribute used to specify the order in which this field is selected compared to other page components when a user presses the Tab key repeatedly. The value of this attribute should be between 0 to 32767.
Syntax:
<apex:inputCheckbox tabindex="1024"></apex:inputCheckbox>
28. title
The title is a string-type attribute that specifies the text to display as a tooltip when the user's mouse pointer hovers over this component.
Syntax:
<apex:inputCheckbox title="Checkbox Title"></apex:inputCheckbox>
29. value
The value is an Object type attribute, i.e., a merge field that references the controller class variable that is associated with this field.
Syntax:
<apex:inputCheckbox value="{!variableName}"></apex:inputCheckbox>
Let's take an example to understand how we can use the <apex: inputCheckbox> component on the Visualforce page.
ApexInputCheckboxExample.vfp
<!-- create apex page with custom controller-->
<apex:page standardController="Contact" extensions="ApexInputCheckboxController" docType="html-5.0">
<!-- use apex form to show the accounts and contacts table-->
<apex:form id="changeStatusForm">
<apex:pageBlock title="Account/Contact Information">
<!-- command buttons are responsible for showing accounts in the table-->
<apex:pageMessages />
<apex:pageBlockSection title="Account Information">
<apex:input value="{!accName}" label="Account Name"/>
<apex:input value="{!accAccountNumber}" label="Account Number"/>
<apex:inputCheckbox value="{!activeRec}"/>
<apex:input value="{!accPhone}" label="Phone"/>
<apex:input value="{!accRating}" label="Rating"/>
<apex:input value="{!accWebsite}" label="Website"/>
</apex:pageBlockSection>
<apex:pageBlockSection title="Contact Information">
<apex:input value="{!conFirstName}" label="First Name"/>
<apex:input value="{!conLastName}" label="Last Name"/>
<apex:input value="{!conAccountId}" label="Account Id"/>
<apex:input value="{!conEmail}" label="Email"/>
<apex:input value="{!conPhone}" label="Phone"/>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:form>
</apex:page>
ApexInputCheckboxController.apxc
// create ApexInputCheckboxController class to get list of Accounts and Contacts
public class ApexInputCheckboxController {
public string accName{get;set;}
public string accAccountNumber{get;set;}
public string accPhone{get;set;}
public string accRating{get;set;}
public string accWebsite{get;set;}
public string conFirstName{get;set;}
public string conLastName{get;set;}
public string conAccountId{get;set;}
public string conEmail{get;set;}
public string conPhone{get;set;}
public boolean activeRec{get;set;}
public String conId {get;set;}
//default constructor to get record Id from the page
public ApexInputCheckboxController(ApexPages.StandardController controller){
conId = controller.getRecord().Id; //'001x00000035SxX' ;
Contact con = [Select Id, FirstName, LastName, AccountId, Email, Phone From Contact Where Id =:conId];
Account acc = [Select Id, Name, AccountNumber, Phone, Rating, Website, Active__c From Account where Id =:con.AccountId];
accName = acc.Name;
accAccountNumber = acc.AccountNumber;
accPhone = acc.Phone;
accRating = acc.Rating;
accWebsite = acc.Website;
conFirstName = con.FirstName;
conLastName = con.LastName;
conAccountId = con.AccountId;
conEmail = con.Email;
conPhone = con.Phone;
if(acc.Active__c == 'Yes'){
activeRec = true;
}else{
activeRec = false;
}
}
}