---
name: "up.fragment.toTarget"
area: "API"
kind: "JavaScript function"
parent: "up.fragment module"
url: "https://unpoly.com/api/up.fragment.toTarget"
---
<nav aria-label="Unpoly docs">[All docs](https://unpoly.com/index.md) · [up.fragment module](https://unpoly.com/api/up.fragment.md)</nav>

# up.fragment.toTarget(element, [options])

[Derives a CSS selector](https://unpoly.com/learn/target-derivation.md) that matches the given element as good as possible.

If no target can be derived and [verified](https://unpoly.com/learn/target-derivation.md#verification), an error `up.CannotTarget` is thrown.

<nav aria-label="Contents">

#### Contents

- [Example](#example)
- [Parameters](#parameters)
- [Return value](#return-value)

</nav>

## Example {#example}

```js
element = up.element.createFromHTML('<span class="klass">...</span>')
selector = up.fragment.toTarget(element) // returns '.klass'
```

---

## Parameters {#parameters}

### Element {#parameters-element}

#### element {#element}

(required)

The element for which to create a selector.

When a string is given, it is returned unchanged.

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

#### [options.strong=false] (Experimental) {#options.strong}

(optional)

Whether to provide a more unique selector by only considering the element's `[id]` and [`[up-id]`](https://unpoly.com/api/up-id.md) attributes.

Weaker target derivers, like the element's class, are not considered in strong mode. The element's tag name is only considered for singleton elements like `<html>` or `<body>`.

Type: boolean

### Verification {#parameters-verification}

#### [options.verify] {#options.verify}

(optional)

Whether to verify that the derived selector matches the given element.

Defaults to [`up.fragment.config.verifyDerivedTarget`](https://unpoly.com/api/up.fragment.config.md#config.verifyDerivedTarget).

Type: boolean

#### [options.origin] {#options.origin}

(optional)

The origin used to [resolve an ambiguous selector](https://unpoly.com/learn/targeting-fragments.md#ambiguous-selectors) during [target verification](https://unpoly.com/learn/target-derivation.md#verification).

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

---

## Return value {#return-value}

The derived selector string.

Type: string

