PreferenceObject: change event

Experimental: This is an experimental technology
Check the Browser compatibility table carefully before using this in production.

Secure context: This feature is available only in secure contexts (HTTPS), in some or all supporting browsers.

The change event of the PreferenceObject interface is fired when a PreferenceObject override value changes. This can happen due to requestOverride or clearOverride method calls.

Syntax

Use the event name in addEventListener():

js
addEventListener("change", (event) => { })

Alternatively, assign to the event handler property of the interface:

js
onchange = (event) => { }

Event type

A generic Event.

Examples

Basic usage

The following snippet logs the user's preferred color scheme to the console when it changes.

js
navigator.preferences.colorScheme.addEventListener("change", (event) => {
  console.log(navigator.preferences.colorScheme.value);
});

Specifications

Specification
Media Queries Level 5
# onchange-attribute

Browser compatibility