HasData
API reference › HTML Attribute › Global › HasData
Used by 4 types
Provides an immutable API for data-* attributes.
See also: https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/data-*.
Methods
Section titled “Methods”| Method | Description | Defined by |
|---|---|---|
addDataAttribute() | Sets a data-* attribute. | HasData |
dataAttributes() | Sets multiple data-* attributes. | HasData |
removeDataAttribute() | Removes a data-* attribute. | HasData |
Method details
Section titled “Method details”addDataAttribute()
Section titled “addDataAttribute() ”public function addDataAttribute(string|UnitEnum $key, bool|Closure(): mixed|float|int|string|Stringable|UnitEnum|null $value): staticSets a data-* attribute.
Parameters
| Parameter | Type | Description |
|---|---|---|
$key | string|UnitEnum | Data attribute key without the data- prefix. |
$value | bool|Closure(): mixed|Stringable|UnitEnum| | Data attribute value, or null to remove the attribute. |
Returns
static: New instance with the updated data-* attribute.
Throws
InvalidArgumentException: if one or more arguments are invalid, of incorrect type or format.
dataAttributes()
Section titled “dataAttributes() ”public function dataAttributes(mixed[] $values): staticSets multiple data-* attributes.
Parameters
| Parameter | Type | Description |
|---|---|---|
$values | mixed[] | Associative array of data keys and values. Values may be scalar, Stringable, UnitEnum, Closure, or null to remove the attribute. |
Returns
static: New instance with the updated data-* attributes.
Throws
InvalidArgumentException: if one or more arguments are invalid, of incorrect type or format.
removeDataAttribute()
Section titled “removeDataAttribute() ”public function removeDataAttribute(string|UnitEnum $key): staticRemoves a data-* attribute.
Parameters
| Parameter | Type | Description |
|---|---|---|
$key | string|UnitEnum | Data attribute key without the data- prefix. |
Returns
static: New instance with the specified data-* attribute removed.