JavaScript/jQuery Switch Options

An object defining configuration properties for the Switch UI component.

accessKey

Specifies the shortcut key that sets focus on the UI component.

Type:

String

| undefined
Default Value: undefined

The value of this property will be passed to the accesskey attribute of the HTML element that underlies the UI component.

activeStateEnabled

Specifies whether the UI component changes its visual state as a result of user interaction.

Type:

Boolean

Default Value: true

The UI component switches to the active state when users press down the primary mouse button. When this property is set to true, the CSS rules for the active state apply. You can change these rules to customize the component.

Use this property when you display the component on a platform whose guidelines include the active state change for UI components.

disabled

Specifies whether the UI component responds to user interaction.

Type:

Boolean

Default Value: false

elementAttr

Specifies the global attributes to be attached to the UI component's container element.

Type:

Object

Default Value: {}

jQuery
$(function(){
    $("#switchContainer").dxSwitch({
        // ...
        elementAttr: {
            id: "elementId",
            class: "class-name"
        }
    });
});
Angular
HTML
TypeScript
<dx-switch ...
    [elementAttr]="{ id: 'elementId', class: 'class-name' }">
</dx-switch>
import { DxSwitchModule } from "devextreme-angular";
// ...
export class AppComponent {
    // ...
}
@NgModule({
    imports: [
        // ...
        DxSwitchModule
    ],
    // ...
})
Vue
App.vue
<template>
    <DxSwitch ...
        :element-attr="switchAttributes">
    </DxSwitch>
</template>

<script>
import DxSwitch from 'devextreme-vue/switch';

export default {
    components: {
        DxSwitch
    },
    data() {
        return {
            switchAttributes: {
                id: 'elementId',
                class: 'class-name'
            }
        }
    }
}
</script>
React
App.js
import React from 'react';

import Switch from 'devextreme-react/switch';

class App extends React.Component {
    switchAttributes = {
        id: 'elementId',
        class: 'class-name'
    }

    render() {
        return (
            <Switch ...
                elementAttr={this.switchAttributes}>
            </Switch>
        );
    }
}
export default App;

focusStateEnabled

Specifies whether the UI component can be focused using keyboard navigation.

Type:

Boolean

Default Value: true (desktop)

height

Specifies the UI component's height.

Type:

Number

|

String

|

Function

| undefined
Return Value:

Number

|

String

The UI component's height.

Default Value: undefined

This property accepts a value of one of the following types:

  • Number
    The height in pixels.

  • String
    A CSS-accepted measurement of height. For example, "55px", "20vh", "80%", "inherit".

  • Function (deprecated since v21.2)
    Refer to the W0017 warning description for information on how you can migrate to viewport units.

NOTE
The property affects only the size of the switch wrapper element. To specify the size of another switch elements, provide the appropriate CSS rules.

hint

Specifies text for a hint that appears when a user pauses on the UI component.

Type:

String

| undefined
Default Value: undefined

hoverStateEnabled

Specifies whether the UI component changes its state when a user pauses on it.

Type:

Boolean

Default Value: true

isDirty

Specifies whether the component's current value differs from the initial value.

Type:

Boolean

Default Value: false

This property is a read-only flag. You can use it to check if the editor value changed.

jQuery
index.js
$(() => {
    const switch = $('#switch').dxSwitch({
        // ...
    }).dxSwitch('instance');

    $('#button').dxButton({
        // ...
        onClick () {
            if (switch.option('isDirty')) {
                DevExpress.ui.notify("Do not forget to save changes", "warning", 500);
            }
        }
    });
});
Angular
app.component.ts
app.component.html
import { Component, ViewChild } from '@angular/core';
import { DxSwitchComponent, DxButtonModule } from 'devextreme-angular';
import notify from 'devextreme/ui/notify';

@Component({
    selector: 'app-root',
    templateUrl: './app.component.html',
    styleUrls: ['./app.component.css']
})
export class AppComponent {
    @ViewChild('switchRef', { static: false }) switch: DxSwitchComponent;

    onClick () {
        if (this.switch.instance.option('isDirty')) {
            notify("Do not forget to save changes", "warning", 500);
        }
    }
}
<dx-switch ... 
    #switchRef
>
</dx-switch>
<dx-button ...
    (onClick)="onClick($event)"
>
</dx-button>
Vue
App.vue
<template>
    <DxSwitch ...
        :ref="switchRef"
    >
    </DxSwitch>
    <DxButton ...
        @click="onClick"
    />
</template>

<script>
import 'devextreme/dist/css/dx.light.css';
import DxSwitch from 'devextreme-vue/switch';
import DxButton from 'devextreme-vue/button';
import notify from 'devextreme/ui/notify';

export default {
    components: {
        DxSwitch,
        DxButton
    },

    data() {
        return {
            switchRef
        }
    },

    methods: {
        onClick () {
            if (this.switch.option('isDirty')) {
                notify("Do not forget to save changes", "warning", 500);
            }
        }
    },

    computed: {
        switch: function() {
            return this.$refs[switchRef].instance;
        }
    }
}
</script>
React
App.js
import React, { useRef } from 'react';
import Switch from 'devextreme-react/switch';
import Button from 'devextreme-react/button';
import 'devextreme/dist/css/dx.light.css';

const App = () => {
    const switchRef = useRef(null);

    const onClick = () => {
        if (this.switchRef.current.instance().option('isDirty')) {
            notify("Do not forget to save changes", "warning", 500);
        }
    };

    return (
        <Switch ...
            ref={switchRef}
        >
        </Switch>
        <Button ...
            onClick={onClick}
        />
    );
};

export default App;
See Also

isValid

Specifies or indicates whether the editor's value is valid.

Type:

Boolean

Default Value: true

NOTE
When you use async rules, isValid is true if the status is "pending" or "valid".
See Also

name

The value to be assigned to the name attribute of the underlying HTML element.

Type:

String

Default Value: ''

Specify this property if the UI component lies within an HTML form that will be submitted.

onContentReady

A function that is executed when the UI component is rendered and each time the component is repainted.

Type:

Function

Function parameters:

Information about the event.

Object structure:
Name Type Description
element

HTMLElement | jQuery

The UI component's container. It is an HTML Element or a jQuery Element when you use jQuery.

component

Switch

The UI component's instance.

Default Value: null

onDisposing

A function that is executed before the UI component is disposed of.

Type:

Function

Function parameters:

Information about the event.

Object structure:
Name Type Description
element

HTMLElement | jQuery

The UI component's container. It is an HTML Element or a jQuery Element when you use jQuery.

component

Switch

The UI component's instance.

Default Value: null

onInitialized

A function used in JavaScript frameworks to save the UI component instance.

Type:

Function

Function parameters:

Information about the event.

Object structure:
Name Type Description
element

HTMLElement | jQuery

The UI component's container. It is an HTML Element or a jQuery Element when you use jQuery.

component

Switch

The UI component's instance.

Default Value: null

Angular
app.component.html
app.component.ts
<dx-switch ...
    (onInitialized)="saveInstance($event)">
</dx-switch>
import { Component } from "@angular/core";
import Switch from "devextreme/ui/data_grid";
// ...
export class AppComponent {
    switchInstance: Switch;
    saveInstance (e) {
        this.switchInstance = e.component;
    }
}
Vue
App.vue (Options API)
App.vue (Composition API)
<template>
    <div>
        <DxSwitch ...
            @initialized="saveInstance">
        </DxSwitch>
    </div>
</template>

<script>
import DxSwitch from 'devextreme-vue/switch';

export default {
    components: {
        DxSwitch
    },
    data: function() {
        return {
            switchInstance: null
        };
    },
    methods: {
        saveInstance: function(e) {
            this.switchInstance = e.component;
        }
    }
};
</script>
<template>
    <div>
        <DxSwitch ...
            @initialized="saveInstance">
        </DxSwitch>
    </div>
</template>

<script setup>
import DxSwitch from 'devextreme-vue/switch';

let switchInstance = null;

const saveInstance = (e) => {
    switchInstance = e.component;
}
</script>
React
App.js
import Switch from 'devextreme-react/switch';

class App extends React.Component {
    constructor(props) {
        super(props);

        this.saveInstance = this.saveInstance.bind(this);
    }

    saveInstance(e) {
        this.switchInstance = e.component;
    }

    render() {
        return (
            <div>
                <Switch onInitialized={this.saveInstance} />
            </div>
        );
    }
}
See Also
jQuery
  • Get a UI component Instance in jQuery
Angular
  • Get a UI component Instance in Angular
Vue
  • Get a UI component Instance in Vue
React
  • Get a UI component Instance in React

onOptionChanged

A function that is executed after a UI component property is changed.

Type:

Function

Function parameters:

Information about the event.

Object structure:
Name Type Description
value any

The modified property's new value.

previousValue any

The UI component's previous value.

name

String

The modified property if it belongs to the first level. Otherwise, the first-level property it is nested into.

fullName

String

The path to the modified property that includes all parent properties.

element

HTMLElement | jQuery

The UI component's container. It is an HTML Element or a jQuery Element when you use jQuery.

component

Switch

The UI component's instance.

Default Value: null

The following example shows how to subscribe to component property changes:

jQuery
index.js
$(function() {
    $("#switchContainer").dxSwitch({
        // ...
        onOptionChanged: function(e) {
            if(e.name === "changedProperty") {
                // handle the property change here
            }
        }
    });
});
Angular
app.component.html
app.component.ts
app.module.ts
<dx-switch ...
    (onOptionChanged)="handlePropertyChange($event)"> 
</dx-switch>
import { Component } from '@angular/core'; 

@Component({ 
    selector: 'app-root', 
    templateUrl: './app.component.html', 
    styleUrls: ['./app.component.css'] 
}) 

export class AppComponent { 
    // ...
    handlePropertyChange(e) {
        if(e.name === "changedProperty") { 
            // handle the property change here
        }
    }
}
import { BrowserModule } from '@angular/platform-browser'; 
import { NgModule } from '@angular/core'; 
import { AppComponent } from './app.component'; 
import { DxSwitchModule } from 'devextreme-angular'; 

@NgModule({ 
    declarations: [ 
        AppComponent 
    ], 
    imports: [ 
        BrowserModule, 
        DxSwitchModule 
    ], 
    providers: [ ], 
    bootstrap: [AppComponent] 
}) 

export class AppModule { }  
Vue
App.vue
<template> 
    <DxSwitch ...
        @option-changed="handlePropertyChange"
    />            
</template> 

<script>  
import 'devextreme/dist/css/dx.light.css'; 
import DxSwitch from 'devextreme-vue/switch'; 

export default { 
    components: { 
        DxSwitch
    }, 
    // ...
    methods: { 
        handlePropertyChange: function(e) {
            if(e.name === "changedProperty") {
                // handle the property change here
            }
        }
    } 
} 
</script> 
React
App.js
import React from 'react';  
import 'devextreme/dist/css/dx.light.css'; 

import Switch from 'devextreme-react/switch'; 

const handlePropertyChange = (e) => {
    if(e.name === "changedProperty") {
        // handle the property change here
    }
}

export default function App() { 
    return ( 
        <Switch ...
            onOptionChanged={handlePropertyChange}
        />        
    ); 
} 

onValueChanged

A function that is executed after the UI component's value is changed.

Type:

Function

Function parameters:

Information about the event.

Object structure:
Name Type Description
value

Object

The UI component's new value.

previousValue

Object

The UI component's previous value.

event

Event (jQuery or EventObject)

The event that caused the function to execute. It is an EventObject or a jQuery.Event when you use jQuery. This field is undefined if the value is changed programmatically.

element

HTMLElement | jQuery

The UI component's container. It is an HTML Element or a jQuery Element when you use jQuery.

component

Switch

The UI component's instance.

Default Value: null

readOnly

Specifies whether the editor is read-only.

Type:

Boolean

Default Value: false

rtlEnabled

Switches the UI component to a right-to-left representation.

Type:

Boolean

Default Value: false

When this property is set to true, the UI component text flows from right to left, and the layout of elements is reversed. To switch the entire application/site to the right-to-left representation, assign true to the rtlEnabled field of the object passed to the DevExpress.config(config) method.

JavaScript
DevExpress.config({
    rtlEnabled: true
});

DataGrid Demo Navigation UI Demo Editors Demo

switchedOffText

Specifies the text displayed when the UI component is switched off.

Type:

String

Default Value: 'OFF'

NOTE
The UI component may or may not display the text depending on the current theme.

switchedOnText

Specifies the text displayed when the UI component is switched on.

Type:

String

Default Value: 'ON'

NOTE
The UI component may or may not display the text depending on the current theme.

tabIndex

Specifies the number of the element when the Tab key is used for navigating.

Type:

Number

Default Value: 0

The value of this property will be passed to the tabindex attribute of the HTML element that underlies the UI component.

validationError

Information on the broken validation rule. Contains the first item from the validationErrors array.

Type: any
Default Value: null

See Also

validationErrors

An array of the validation rules that failed.

Type:

Array<any>

Default Value: null

validationMessageMode

Specifies how the message about the validation rules that are not satisfied by this editor's value is displayed.

Default Value: 'auto'

The following property values are possible:

  • auto
    The tooltip with the message is displayed when the editor is in focus.
  • always
    The tooltip with the message is not hidden when the editor loses focus.

validationMessagePosition

Specifies the position of a validation message relative to the component. The validation message describes the validation rules that this component's value does not satisfy.

Type:

Position

Default Value: 'bottom'

The following example positions a validation message at the component's right:

jQuery
index.js
$(function() {
    $("#switchContainer").dxSwitch({
        // ...
        validationMessagePosition: 'right'
    }).dxValidator({
        validationRules: [{
            type: 'required',
            message: 'Required',
        }],
    });
});
Angular
app.component.html
<dx-switch ...
    validationMessagePosition="right">
    <dx-validator>
        <dxi-validation-rule
            type="required"
            message="Required"
        >
        </dxi-validation-rule>
    </dx-validator>
</dx-switch>
Vue
App.vue
<template>
    <DxSwitch ...
        validation-message-position="right"
    >
        <DxValidator>
            <DxRequiredRule message="Required" />
        </DxValidator>
    </DxSwitch>
</template>

<script>
    // ...
</script>
React
App.js
import React from 'react';
// ...

function App() {
    return (
        <Switch ...
            validationMessagePosition="right"
        >
            <Validator>
                <RequiredRule message="Required" />
            </Validator>
        </Switch>
    ); 

};
export default App;

validationStatus

Indicates or specifies the current validation status.

Default Value: 'valid'

The following table illustrates the validation status indicators:

validationStatus Indicator
"pending" DevExtreme editor validation status: pending
"valid" DevExtreme editor validation status: valid
"invalid" DevExtreme editor validation status: invalid

When you assign "invalid" to validationStatus, you can also use the validationErrors array to set an error message as shown below:

jQuery
index.js
$(function() {
    const switch = $("#switchContainer").dxSwitch({
        // ...
    }).dxSwitch("instance");

    function setInvalidStatus(message) {
        switch.option({
            validationStatus: "invalid",
            validationErrors: [{ message: message }]
        });
    }
});
Angular
app.component.html
app.component.ts
<dx-switch
    [validationStatus]="validationStatus"
    [validationErrors]="validationErrors">
</dx-switch>
// ...
export class AppComponent {
    validationStatus: string = "valid";
    validationErrors: any;
    // ...
    setInvalidStatus(message) {
        this.validationStatus = "invalid";
        this.validationErrors = [{ message: message }];
    }
}
Vue
App.vue
<template>
    <DxSwitch ...
        :validation-status="validationStatus"
        :validation-errors="validationErrors"
    />
</template>

<script>
    // ...
    export default {
        // ...
        data() {
            return {
                validationStatus: "valid",
                validationErrors: []
            }
        },
        methods: {
            setInvalidStatus(message) {
                this.validationStatus = "invalid";
                this.validationErrors = [{ message: message }];
            }
        }
    }
</script>
React
App.js
import React, { useState } from 'react';
// ...

function App() {
    const [validationStatus, setValidationStatus] = useState("valid");
    const [validationErrors, setValidationErrors] = useState([]);

    const setInvalidStatus = message => {
        setValidationStatus("invalid");
        setValidationErrors([{ message: message }]);
    }

    return (
        <Switch
            validationStatus={validationStatus}
            validationErrors={validationErrors}
        />
    ); 

};
export default App;

value

A Boolean value specifying whether the current switch state is "On" or "Off".

Type:

Boolean

Default Value: false
Raised Events: onValueChanged

visible

Specifies whether the UI component is visible.

Type:

Boolean

Default Value: true

width

Specifies the UI component's width.

Type:

Number

|

String

|

Function

| undefined
Return Value:

Number

|

String

The UI component's width.

Default Value: undefined

This property accepts a value of one of the following types:

  • Number
    The width in pixels.

  • String
    A CSS-accepted measurement of width. For example, "55px", "20vw", "80%", "auto", "inherit".

  • Function (deprecated since v21.2)
    Refer to the W0017 warning description for information on how you can migrate to viewport units.