Skip to content
PHP Ecosystem
Explore

HasData

API reference › HTML Attribute › Global › HasData

traitUIAwesome\Html\Attribute\Global\HasData
Sourcesrc/Global/HasData.php
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-*.

MethodDescriptionDefined by
addDataAttribute()Sets a data-* attribute.HasData
dataAttributes()Sets multiple data-* attributes.HasData
removeDataAttribute()Removes a data-* attribute.HasData
public function addDataAttribute(string|UnitEnum $key, bool|Closure(): mixed|float|int|string|Stringable|UnitEnum|null $value): static

Sets a data-* attribute.

Parameters

ParameterTypeDescription
$keystring|UnitEnumData attribute key without the data- prefix.
$valuebool|Closure(): mixed|float|int|string|Stringable|UnitEnum|nullData attribute value, or null to remove the attribute.

Returns

static: New instance with the updated data-* attribute.

Throws

public function dataAttributes(mixed[] $values): static

Sets multiple data-* attributes.

Parameters

ParameterTypeDescription
$valuesmixed[]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

public function removeDataAttribute(string|UnitEnum $key): static

Removes a data-* attribute.

Parameters

ParameterTypeDescription
$keystring|UnitEnumData attribute key without the data- prefix.

Returns

static: New instance with the specified data-* attribute removed.