All docs
V19.2
24.1
The page you are viewing does not exist in version 24.1.
23.2
The page you are viewing does not exist in version 23.2.
23.1
The page you are viewing does not exist in version 23.1.
22.2
The page you are viewing does not exist in version 22.2.
22.1
The page you are viewing does not exist in version 22.1.
21.2
The page you are viewing does not exist in version 21.2.
21.1
The page you are viewing does not exist in version 21.1.
20.2
The page you are viewing does not exist in version 20.2.
20.1
The page you are viewing does not exist in version 20.1.
19.2
19.1
18.2
18.1
17.2
A newer version of this page is available. Switch to the current version.

DevExtreme jQuery - Handle the Value Change Event

To process a new Switch value, you need to handle the value change event. If the handling function is not going to be changed during the lifetime of the widget, assign it to the onValueChanged option when you configure the widget.

jQuery
index.js
$(function() {
    $("#switchContainer").dxSwitch({
        onValueChanged: function (e) {
            var previousValue = e.previousValue;
            var newValue = e.value;
            // Event handling commands go here
        }
    });
});
Angular
app.component.html
app.component.ts
app.module.ts
<dx-switch
    [(value)]="switchValue"
    (onValueChanged)="switchValueChanged($event)">
</dx-switch>
import { Component } from '@angular/core';

@Component({
    selector: 'app-root',
    templateUrl: './app.component.html',
    styleUrls: ['./app.component.css']
})
export class AppComponent {
    switchValue: boolean = true;

    switchValueChanged(e) {
        const previousValue = e.previousValue;
        const newValue = e.value;
        // Event handling commands go 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
        v-model="switchValue"
        @value-сhanged="switchValueChanged"
    />
</template>

<script>
import 'devextreme/dist/css/dx.common.css';
import 'devextreme/dist/css/dx.light.css';

import DxSwitch from 'devextreme-vue/switch';

export default {
    components: {
        DxSwitch
    },
    data() {
        switchValue: true
    },
    methods: {
        switchValueChanged(e) {
            const previousValue = e.previousValue;
            const newValue = e.value;
            // Event handling commands go here
        }
    }
}
</script>
React
App.js
import React from 'react';

import 'devextreme/dist/css/dx.common.css';
import 'devextreme/dist/css/dx.light.css';

import Switch from 'devextreme-react/switch';

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

        this.state = {
            switchValue: true
        };

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

    render() {
        return (
            <Switch
                value={this.switchValue}
                onValueChanged={this.switchValueChanged} />
        );
    }

    switchValueChanged(e) {
        const previousValue = e.previousValue;
        const newValue = e.value;
        // Event handling commands go here
        this.setState({ switchValue: newValue });
    }
}
export default App;

If you are going to change event handlers at runtime, or if you need to attach several handlers to the value change event, subscribe to this event using the on(eventName, eventHandler) method.

JavaScript
var valueChangedHandler1 = function (e) {
    var previousValue = e.previousValue;
    var newValue = e.value;
    // First handler of the "valueChanged" event
};

var valueChangedHandler2 = function (e) {
    var previousValue = e.previousValue;
    var newValue = e.value;
    // Second handler of the "valueChanged" event
};

$("#switchContainer").dxSwitch("instance")
    .on("valueChanged", valueChangedHandler1)
    .on("valueChanged", valueChangedHandler2);
See Also