---
title: Element
summary: Represents an HTML element encountered during rewriting. Passed to the callback registered via HTMLRewritingStream.onElement.
url: https://www.fastly.com/documentation/reference/compute/sdks/javascript/js-compute/html-rewriter/class.Element/
---

**Kind:** Class  
**Module:** [html-rewriter](https://www.fastly.com/documentation/reference/compute/sdks/javascript/js-compute/html-rewriter/)  
**Package:** @fastly/js-compute 4.0.1  
**Since:** 3.35.0  
**Source:** https://github.com/fastly/js-compute-runtime/blob/70cf30af4e657879b2d1e7eafab880e39e3a3f7d/types/html-rewriter.d.ts#L101

Represents an HTML element encountered during rewriting. Passed to the callback registered
via [HTMLRewritingStream.onElement](https://www.fastly.com/documentation/reference/compute/sdks/javascript/js-compute/html-rewriter/class.HTMLRewritingStream/#method-onElement).

## Properties

### selector

**Type:** `string`  
**Flags:** read-only  
**Source:** https://github.com/fastly/js-compute-runtime/blob/70cf30af4e657879b2d1e7eafab880e39e3a3f7d/types/html-rewriter.d.ts#L105

The CSS selector that matched this element.

### tag

**Type:** `string`  
**Flags:** read-only  
**Source:** https://github.com/fastly/js-compute-runtime/blob/70cf30af4e657879b2d1e7eafab880e39e3a3f7d/types/html-rewriter.d.ts#L109

The tag name of this element.

## Methods

### after

**Source:** https://github.com/fastly/js-compute-runtime/blob/70cf30af4e657879b2d1e7eafab880e39e3a3f7d/types/html-rewriter.d.ts#L151

```javascript
after(content: string, options?: ElementRewriterOptions): void
```

Inserts content after the element's closing tag.

**Parameters**

- `content`: `string` — HTML or text to insert
- `options`: `ElementRewriterOptions` (optional) — Optional rewriting options

**Returns**

`void`

### append

**Source:** https://github.com/fastly/js-compute-runtime/blob/70cf30af4e657879b2d1e7eafab880e39e3a3f7d/types/html-rewriter.d.ts#L163

```javascript
append(content: string, options?: ElementRewriterOptions): void
```

Inserts content at the end of the element's children.

**Parameters**

- `content`: `string` — HTML or text to append
- `options`: `ElementRewriterOptions` (optional) — Optional rewriting options

**Returns**

`void`

### before

**Source:** https://github.com/fastly/js-compute-runtime/blob/70cf30af4e657879b2d1e7eafab880e39e3a3f7d/types/html-rewriter.d.ts#L145

```javascript
before(content: string, options?: ElementRewriterOptions): void
```

Inserts content before the element's opening tag.

**Parameters**

- `content`: `string` — HTML or text to insert
- `options`: `ElementRewriterOptions` (optional) — Optional rewriting options

**Returns**

`void`

### getAttribute

**Source:** https://github.com/fastly/js-compute-runtime/blob/70cf30af4e657879b2d1e7eafab880e39e3a3f7d/types/html-rewriter.d.ts#L122

```javascript
getAttribute(name: string): string | null
```

Gets the value of an attribute.

**Parameters**

- `name`: `string` — Attribute name

**Returns**

`string | null`

The attribute value, or `null` if the attribute is not present.

### prepend

**Source:** https://github.com/fastly/js-compute-runtime/blob/70cf30af4e657879b2d1e7eafab880e39e3a3f7d/types/html-rewriter.d.ts#L157

```javascript
prepend(content: string, options?: ElementRewriterOptions): void
```

Inserts content at the beginning of the element's children.

**Parameters**

- `content`: `string` — HTML or text to prepend
- `options`: `ElementRewriterOptions` (optional) — Optional rewriting options

**Returns**

`void`

### removeAttribute

**Source:** https://github.com/fastly/js-compute-runtime/blob/70cf30af4e657879b2d1e7eafab880e39e3a3f7d/types/html-rewriter.d.ts#L127

```javascript
removeAttribute(name: string): void
```

Removes an attribute from the element.

**Parameters**

- `name`: `string` — Attribute name

**Returns**

`void`

### replaceChildren

**Source:** https://github.com/fastly/js-compute-runtime/blob/70cf30af4e657879b2d1e7eafab880e39e3a3f7d/types/html-rewriter.d.ts#L139

```javascript
replaceChildren(content: string, options?: ElementRewriterOptions): void
```

Replaces the element's children with new content, keeping the element's tags.

**Parameters**

- `content`: `string` — Replacement HTML or text
- `options`: `ElementRewriterOptions` (optional) — Optional rewriting options

**Returns**

`void`

### replaceWith

**Source:** https://github.com/fastly/js-compute-runtime/blob/70cf30af4e657879b2d1e7eafab880e39e3a3f7d/types/html-rewriter.d.ts#L133

```javascript
replaceWith(content: string, options?: ElementRewriterOptions): void
```

Replaces the element and its children with new content.

**Parameters**

- `content`: `string` — Replacement HTML or text
- `options`: `ElementRewriterOptions` (optional) — Optional rewriting options

**Returns**

`void`

### setAttribute

**Source:** https://github.com/fastly/js-compute-runtime/blob/70cf30af4e657879b2d1e7eafab880e39e3a3f7d/types/html-rewriter.d.ts#L116

```javascript
setAttribute(name: string, value: string): void
```

Sets an attribute on the element. If the attribute already exists, its value is updated.

**Parameters**

- `name`: `string` — Attribute name
- `value`: `string` — Attribute value

**Returns**

`void`
