wp_js_dataset_name
wp_js_dataset_name ( $html_attribute_name )
Parameters:- (string) html_attribute_name Raw attribute name as found in the source HTML.
See:Returns:- (string|null) Transformed `dataset` name, if interpretable as a custom data attribute, else `null`.
Defined at: - Introduced in WordPress: 6.9.0
- Deprecated in WordPress: —
Description
Return the corresponding JavaScript `dataset` name for an attribute
if it represents a custom data attribute, or `null` if not.Custom data attributes appear in an element's `dataset` property in a
browser, but there's a specific way the names are translated from HTML
into JavaScript. This function indicates how the name would appear in
JavaScript if a browser would recognize it as a custom data attribute.
Example:
// Dash-letter pairs turn into capital letters.
'postId' === wp_js_dataset_name( 'data-post-id' );
'Before' === wp_js_dataset_name( 'data--before' );
'-One--Two---' === wp_js_dataset_name( 'data---one---two---' );
// Not every attribute name will be interpreted as a custom data attribute.
null === wp_js_dataset_name( 'post-id' );
null === wp_js_dataset_name( 'data' );
// Some very surprising names will; for example, a property whose name is the empty string.
'' === wp_js_dataset_name( 'data-' );
0 === strlen( wp_js_dataset_name( 'data-' ) );