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

# up.element.attr(element, attribute)

Returns the given `attribute` value for the given `element`.

If the element does not have the given attribute, it returns `undefined`. This is a difference from the native `Element#getAttribute()`, which [mostly returns `null` in that case](https://developer.mozilla.org/en-US/docs/Web/API/Element/getAttribute#Non-existing_attributes).

If the element has the attribute but without a value (e.g. `<input readonly>`), it returns an empty string.

---

## Parameters {#parameters}

### element {#element}

(required)

The element from which to read an attribute.

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

### attribute {#attribute}

(required)

The name of the attribute to read.

Type: string

---

## Return value {#return-value}

The attribute value.

Returns `undefined` if the attribute is not set on the given element.

Type: string | undefined

