Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New pages: DOMTokenList and MediaList toString() method #37052

Merged
merged 13 commits into from
Dec 21, 2024
2 changes: 2 additions & 0 deletions files/en-us/web/api/domtokenlist/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ A `DOMTokenList` is indexed beginning with `0` as with JavaScript {{jsxref("Arra
- : Executes a provided callback function once for each `DOMTokenList` element.
- {{domxref("DOMTokenList.keys()")}}
- : Returns an {{jsxref("Iteration_protocols", "iterator", "", 1)}}, allowing you to go through all keys of the key/value pairs contained in this object.
- {{domxref("DOMTokenList.toString()")}}
- : Returns the {{domxref("DOMTokenList.value")}}, the space-separated values of the list as a string.
- {{domxref("DOMTokenList.values()")}}
- : Returns an {{jsxref("Iteration_protocols", "iterator", "", 1)}}, allowing you to go through all values of the key/value pairs contained in this object.

Expand Down
51 changes: 51 additions & 0 deletions files/en-us/web/api/domtokenlist/tostring/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
---
title: "DOMTokenList: toString() method"
short-title: toString()
slug: Web/API/DOMTokenList/toString
page-type: web-api-instance-method
browser-compat: api.DOMTokenList.toString
---

{{APIRef("DOM")}}

The **`toString()`** {{Glossary("stringifier")}} method of the {{domxref("DOMTokenList")}} interface returns the values of the token list serialized as a string. The return value is equal to the {{domxref("DOMTokenList.value")}} property.
estelle marked this conversation as resolved.
Show resolved Hide resolved

## Syntax

```js-nolint
toString()
```

### Parameters

None.

### Return value

A string; a space-separated list of values.
estelle marked this conversation as resolved.
Show resolved Hide resolved

## Examples

```js
const element = document.createElement("div");
const classes = element.classList;

element.className = "shop empty-cart";
classes.add("logged-in", "dark-mode");

console.log(classes.toString());
// "shop empty-cart logged-in dark-mode"
```

## Specifications

{{Specifications}}

## Browser compatibility

{{Compat}}

## See also

- {{domxref("DOMTokenList")}}
estelle marked this conversation as resolved.
Show resolved Hide resolved
estelle marked this conversation as resolved.
Show resolved Hide resolved
- {{domxref("DOMTokenList.add()")}}
2 changes: 2 additions & 0 deletions files/en-us/web/api/medialist/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ The **`MediaList`** interface represents the media queries of a stylesheet, e.g.
- : Removes a media query from the `MediaList`.
- {{domxref("MediaList.item()")}}
- : A getter that returns a string representing a media query as text, given the media query's index value inside the `MediaList`. This method can also be called using the bracket (`[]`) syntax.
- {{domxref("MediaList.toString()")}}
- : Returns the {{domxref("MediaList.mediaText")}}, a string representing the `MediaList` as text, the value separated by a comma and space.
estelle marked this conversation as resolved.
Show resolved Hide resolved

## Examples

Expand Down
57 changes: 57 additions & 0 deletions files/en-us/web/api/medialist/tostring/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
---
title: "MediaList: toString() method"
short-title: toString()
slug: Web/API/MediaList/toString
page-type: web-api-instance-method
browser-compat: api.MediaList.toString
---

{{APIRef("CSSOM")}}

The **`toString()`** {{Glossary("stringifier")}} method of the {{domxref("MediaList")}} API returns a string representing the array-like `MediaList` object values, a list of one or more media formats, as text. The return value is the same as {{domxref("MediaList.mediaText")}} stringifier property.
estelle marked this conversation as resolved.
Show resolved Hide resolved

## Syntax

```js-nolint
toString()
```

### Parameters

None.

### Return value

A string; a space-separated list of media values.
estelle marked this conversation as resolved.
Show resolved Hide resolved

## Examples

```js
const SS = document.styleSheets[0]; // the document's first stylesheet
const ML = SS.media; // the mediaList of the stylesheet
// set the `media` text to a media query value
ss.mediaText = "SCREEN AND (140PX <= WIDTH <= 380PX)";
// add a second media value
ML.appendMedium("SCREEN AND (MAX-HEIGHT: 400PX) AND (ORIENTATION: LANDSCAPE))");
// erroniously, add a media query twice
ML.appendMedium("SCREEN AND (MAX-HEIGHT: 400PX) AND (ORIENTATION: LANDSCAPE))");
console.log(ML.toString()); // "screen and (140px <= width <= 380px), screen and (max-height: 400px) and (orientation: landscape)"
estelle marked this conversation as resolved.
Show resolved Hide resolved
```

Media list values are lowercase. When stringified, the values are separated by a comma followed by a space.

estelle marked this conversation as resolved.
Show resolved Hide resolved
## Specifications

{{Specifications}}

## Browser compatibility

{{Compat}}

## See also

- {{domxref("MediaList.mediaText")}}
- {{domxref("MediaList.appendMedium()")}}
- {{domxref("MediaList")}}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again I doubt the value of see-alsoing the interface or its other members.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I include links to the methods used in the example within

estelle marked this conversation as resolved.
Show resolved Hide resolved
- [Media queries](/en-US/docs/Web/CSS/CSS_media_queries)
- [Using media queries](/en-US/docs/Web/CSS/CSS_media_queries/Using_media_queries)
Loading