---
name: "up.element.booleanAttr"
area: "API"
kind: "JavaScript function"
parent: "up.element module"
url: "https://unpoly.com/api/up.element.booleanAttr"
---
<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.booleanAttr(element, attribute)

Returns the value of the given attribute on the given element, cast as a boolean value.

If the attribute value cannot be cast to `true` or `false`, `undefined` is returned.

<nav aria-label="Contents">

#### Contents

- [Casting rules](#casting-rules)
- [Parameters](#parameters)
- [Return value](#return-value)

</nav>

## Casting rules {#casting-rules}

This function deviates from the [HTML Standard for boolean attributes](https://html.spec.whatwg.org/multipage/common-microsyntaxes.html#boolean-attributes) to allow `undefined` values. When an attribute is missing, Unpoly considers the value to be `undefined` (whereas the standard would assume `false`).

Unpoly also allows `"true"` and `"false"` as attribute values.

The table below shows return values for `up.element.booleanAttr(element, 'foo')` given different elements:

| Element | Return value |
| --- | --- |
| `<div foo>` | `true` |
| `<div foo="foo">` | `true` |
| `<div foo="true">` | `true` |
| `<div foo="">` | `true` |
| `<div foo="false">` | `false` |
| `<div>` | `undefined` |
| `<div foo="bar">` | `true` |

---

## Parameters {#parameters}

### element {#element}

(required)

The element from which to retrieve the attribute value.

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

### attribute {#attribute}

(required)

The attribute name.

Type: string

---

## Return value {#return-value}

The cast attribute value.

Type: boolean | undefined

