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

# up.focus(element, [options])

Focuses the given element.

Focusing an element will also [reveal](https://unpoly.com/api/up.reveal.md) it, unless `{ preventScroll: true }` is passed.

<nav aria-label="Guides">

- [Learn: Controlling focus](https://unpoly.com/learn/focus.md#js)

</nav>

---

## Parameters {#parameters}

### element {#element}

(required)

The element to focus.

Type: string | [Element](https://developer.mozilla.org/en-US/docs/Web/API/Element) | [jQuery](https://learn.jquery.com/using-jquery-core/jquery-object/)

### [options.preventScroll=false] {#options.preventScroll}

(optional)

Whether to prevent changes to the scroll position.

Type: boolean

### [options.focusVisible='auto'] (Experimental) {#options.focusVisible}

(optional)

Whether the focused element should have a [visible focus ring](https://unpoly.com/learn/focus-visibility.md).

If set to `true` the element will be assigned the [`.up-focus-visible`](https://unpoly.com/api/up-focus-visible.md) class.

If set to `false` the element will be assigned the [`.up-focus-hidden`](https://unpoly.com/api/up-focus-hidden.md) class.

If set to `'auto'` (the default), focus will be visible if [`up.viewport.config.autoFocusVisible()`](https://unpoly.com/api/up.viewport.config.md#config.autoFocusVisible) returns `true` for the given element and [current input device](https://unpoly.com/api/up.event.inputDevice.md).

The [`:focus-visible`](https://developer.mozilla.org/en-US/docs/Web/CSS/:focus-visible) pseudo-class will also be set or removed accordingly on [supporting browsers](https://caniuse.com/mdn-api_htmlelement_focus_options_focusvisible_parameter).

Type: boolean | string

### [options.force=false] {#options.force}

(optional)

Whether to force focus even if `element` would otherwise not be a focusable element.

Type: boolean

