ToggleButton

ToggleButton is used to select a boolean value using a button.


import { ToggleButtonModule } from 'primeng/togglebutton';

Two-way binding to a boolean property is defined using the standard ngModel directive.

No

<p-toggleButton [(ngModel)]="checked" onLabel="Yes" offLabel="No"></p-toggleButton>

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

No

<form [formGroup]="formGroup">
    <p-toggleButton formControlName="checked" onLabel="Yes" offLabel="No"></p-toggleButton>
</form>

Icons and Labels can be customized using onLabel, offLabel, onIcon and offIcon properties.

I reject

<p-toggleButton [(ngModel)]="checked" onLabel="I confirm" offLabel="I reject" onIcon="pi pi-check" offIcon="pi pi-times" [style]="{ width: '10em' }"></p-toggleButton>

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

No

<p-toggleButton disabled="true" onIcon="pi pi-check" offIcon="pi pi-times" [(ngModel)]="checked" onLabel="Yes" offLabel="No" styleClass="w-full sm:w-10rem" aria-label="Confirmation"></p-toggleButton>

Following is the list of structural style classes, for theming classes visit theming page.

NameElement
p-togglebuttonContainer element.
p-button-icon-leftText element.
p-button-icon-rightValue element.

Screen Reader

ToggleButton component uses an element with button role and updates aria-pressed state for screen readers. Value to describe the component can be defined with ariaLabelledBy or ariaLabel props, it is highly suggested to use either of these props as the component changes the label displayed which will result in screen readers to read different labels when the component receives focus. To prevent this, always provide an aria label that does not change related to state.


<span id="rememberme">Remember Me</span>
<p-toggleButton ariaLabelledBy="rememberme"></p-toggleButton>

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

Keyboard Support

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