---
name: "up.Preview.prototype.addClass"
area: "API"
kind: "Instance method"
parent: "up.Preview class"
url: "https://unpoly.com/api/up.Preview.prototype.addClass"
---
<nav aria-label="Unpoly docs">[All docs](https://unpoly.com/index.md) · [up.Preview class](https://unpoly.com/api/up.Preview.md)</nav>

# up.Preview.prototype.addClass([element], className)

Temporarily adds a CSS class to an element.

The class will be removed once the preview [ends](https://unpoly.com/learn/previews.md#ending).

## Example {#example}

This preview sets a temporary `.loading` class on the targeted fragment:

```js
up.preview('loading-class', function(preview) {
  preview.addClass('loading')
})
```

---

## Parameters {#parameters}

### [element=this.fragment] {#element}

(optional)

The element or selector to change.

If omitted, the [targeted fragment](https://unpoly.com/api/up.Preview.prototype.fragment.md) will be changed.

Type: [Element](https://developer.mozilla.org/en-US/docs/Web/API/Element) | string

### className {#className}

(required)

The CSS class to add.

Type: string

