MediaQueryList: change event

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since September 2020.

The change event of the MediaQueryList interface fires when the status of media query support changes.

Syntax

Use the event name in methods like addEventListener(), or set an event handler property.

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

onchange = (event) => { }

Event type

A MediaQueryListEvent. Inherits from Event.

Event MediaQueryListEvent

Event properties

The MediaQueryListEvent interface inherits properties from its parent interface, Event.

MediaQueryListEvent.matches Read only

A boolean value that is true if the document currently matches the media query list, or false if not.

MediaQueryListEvent.media Read only

A string representing a serialized media query.

Example

js
const mql = window.matchMedia("(width <= 600px)");

mql.onchange = (e) => {
  if (e.matches) {
    /* the viewport is 600 pixels wide or less */
    console.log("This is a narrow screen — less than 600px wide.");
  } else {
    /* the viewport is more than 600 pixels wide */
    console.log("This is a wide screen — more than 600px wide.");
  }
};

Specifications

Specification
CSSOM View Module
# dom-mediaquerylist-onchange

Browser compatibility

desktop mobile
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
change event
Chrome – Full support
Chrome 39 (Release date: 2014-11-18)
footnote Full support
Edge – Full support
Edge 79 (Release date: 2020-01-15)
footnote Full support
Firefox – Full support
Firefox 55 (Release date: 2017-08-08)
footnote Full support
Opera – Full support
Opera 26 (Release date: 2014-12-03)
footnote Full support
Safari – Full support
Safari 14 (Release date: 2020-09-16)
footnote Full support
Chrome Android – Full support
Chrome Android 39 (Release date: 2014-11-12)
footnote Full support
Firefox for Android – Full support
Firefox for Android 55 (Release date: 2017-08-08)
footnote Full support
Opera Android – Full support
Opera Android 26 (Release date: 2014-12-02)
footnote Full support
Safari on iOS – Full support
Safari on iOS 14 (Release date: 2020-09-16)
footnote Full support
Samsung Internet – Full support
Samsung Internet 4 (Release date: 2016-03-11)
footnote Full support
WebView Android – Full support
WebView Android 39 (Release date: 2014-11-12)
footnote Full support
WebView on iOS – Full support
WebView on iOS 14 (Release date: 2020-09-16)
footnote Full support

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support

See also