# HasEvents

> Provides an immutable API for on* event attributes.

[API reference](/docs/api/index.md) › [HTML Attribute](/docs/extensions/html-attribute/reference/index.md) › [Global](/docs/extensions/html-attribute/reference/global/index.md) › HasEvents

| trait | `UIAwesome\Html\Attribute\Global\HasEvents` |
| --- | --- |
| Source | [`src/Global/HasEvents.php`](https://github.com/ui-awesome/html-attribute/blob/main/src/Global/HasEvents.php#L20) |

Used by 4 types:

- [`UIAwesome\Html\Core\Element\BaseBlock`](/docs/extensions/html-core/reference/element/baseblock/index.md)
- [`UIAwesome\Html\Core\Element\BaseInline`](/docs/extensions/html-core/reference/element/baseinline/index.md)
- [`UIAwesome\Html\Core\Element\BaseInput`](/docs/extensions/html-core/reference/element/baseinput/index.md)
- [`UIAwesome\Html\Core\Element\BaseVoid`](/docs/extensions/html-core/reference/element/basevoid/index.md)

Provides an immutable API for `on*` event attributes.

See also: <https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes#event_handler_attributes>.

## Methods

| Method | Description | Defined by |
| --- | --- | --- |
| [`addEvent()`](#method-addEvent) | Sets an `on*` event attribute. | `HasEvents` |
| [`events()`](#method-events) | Sets multiple `on*` event attributes. | `HasEvents` |
| [`removeEvent()`](#method-removeEvent) | Removes an `on*` event attribute. | `HasEvents` |

## Method details

### addEvent() <span id="method-addEvent"></span>

```php
public function addEvent(string|UnitEnum $key, Closure(): mixed|string|Stringable|UnitEnum|null $value): static
```

Sets an `on*` event attribute.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$key` | `string\|`[`UnitEnum`](https://www.php.net/manual/en/class.unitenum.php) | Event attribute key with or without the leading `on` prefix. |
| `$value` | [`Closure`](https://www.php.net/manual/en/class.closure.php)`(): mixed\|string\|`[`Stringable`](https://www.php.net/manual/en/class.stringable.php)`\|`[`UnitEnum`](https://www.php.net/manual/en/class.unitenum.php)`\|null` | JavaScript handler code, or `null` to remove the attribute. |

**Returns**

`static`: New instance with the updated `on*` event attribute.

**Throws**

- [`InvalidArgumentException`](https://www.php.net/manual/en/class.invalidargumentexception.php): if one or more arguments are invalid, of incorrect type or format.

### events() <span id="method-events"></span>

```php
public function events(mixed[] $values): static
```

Sets multiple `on*` event attributes.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$values` | `mixed[]` | Associative array of event keys and handlers. Values may be Stringable, Closure, string, or `null` to remove the attribute. |

**Returns**

`static`: New instance with the updated `on*` event attributes.

**Throws**

- [`InvalidArgumentException`](https://www.php.net/manual/en/class.invalidargumentexception.php): if one or more arguments are invalid, of incorrect type or format.

### removeEvent() <span id="method-removeEvent"></span>

```php
public function removeEvent(string|UnitEnum $key): static
```

Removes an `on*` event attribute.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$key` | `string\|`[`UnitEnum`](https://www.php.net/manual/en/class.unitenum.php) | Event name or enum case to remove. |

**Returns**

`static`: New instance with the specified `on*` event attribute removed.

**Throws**

- [`InvalidArgumentException`](https://www.php.net/manual/en/class.invalidargumentexception.php): if the event key is invalid.