Checkbox

Checkbox is an extension to standard checkbox element with theming.


import { CheckboxModule } from 'primeng/checkbox';

Binary checkbox is used as a controlled input with ngModel and binary properties.


<p-checkbox 
    [(ngModel)]="checked" 
    [binary]="true" 
    inputId="binary" />

When indeterminate is present, the checkbox masks the actual value visually.


<p-checkbox 
    [(ngModel)]="checked" 
    [binary]="true"
    [indeterminate]="true"
    inputId="binary" />

Checkbox can also be used with reactive forms. In this case, the formControlName property is used to bind the component to a form control.


<form class="flex items-center gap-1" [formGroup]="formGroup">
    <p-checkbox 
        formControlName="city" 
        value="New York" 
        inputId="ny" />
    <label for="ny">New York</label>
</form>

Multiple checkboxes can be grouped together.


<div class="flex items-center">
    <p-checkbox 
        inputId="ingredient1" 
        name="pizza"
        value="Cheese" 
        [(ngModel)]="pizza" 
    />
    <label for="ingredient1" class="ml-2"> Cheese </label>
</div>
<div class="flex items-center">
    <p-checkbox 
        inputId="ingredient2" 
        name="pizza" 
        value="Mushroom" 
        [(ngModel)]="pizza" 
    />
    <label for="ingredient2" class="ml-2"> Mushroom </label>
</div>
<div class="flex items-center">
    <p-checkbox 
        inputId="ingredient3" 
        name="pizza" 
        value="Pepper" 
        [(ngModel)]="pizza" 
    />
    <label for="ingredient3" class="ml-2"> Pepper </label>
</div>
<div class="flex items-center">
    <p-checkbox 
        inputId="ingredient4" 
        name="pizza" 
        value="Onion" 
        [(ngModel)]="pizza" 
    />
    <label for="ingredient4" class="ml-2"> Onion </label>
</div>

Checkboxes can be generated using a list of values.


<div *ngFor="let category of categories" class="field-checkbox">
    <p-checkbox 
        [inputId]="category.key" 
        name="group" 
        [value]="category" 
        [(ngModel)]="selectedCategories" 
    />
    <label [for]="category.key" class="ml-2"> {{ category.name }} </label>
</div>

Invalid state style is added using the ng-invalid and ng-dirty class to indicate a failed validation.


<p-checkbox 
    [(ngModel)]="checked"
    [binary]="true"
    inputId="binary"
    class="ng-invalid ng-dirty" />

Specify the variant property as filled to display the component with a higher visual emphasis than the default outlined style.


<p-checkbox 
    [(ngModel)]="checked" 
    [binary]="true" 
    variant="filled" />

When disabled is present, the element cannot be edited and focused.


<p-checkbox 
    [disabled]="true" 
    [(ngModel)]="checked" />

Screen Reader

Checkbox component uses a hidden native checkbox element internally that is only visible to screen readers. Value to describe the component can either be provided via label tag combined with inputId prop or using ariaLabelledBy, ariaLabel props.


<label for="chkbox1">Remember Me</label>
<p-checkbox inputId="chkbox1"/>

<span id="chkbox2">Remember Me</span>
<p-checkbox ariaLabelledBy="chkbox2"/>

<p-checkbox ariaLabel="Remember Me"/>

Keyboard Support

KeyFunction
tabMoves focus to the checkbox.
spaceToggles the checked state.