We can use the querySelectorAll
method to select all elements that have a specific data attribute. Here's an example:
index.tsx77 chars2 lines
This will find all the elements that have a data-attribute-name
attribute with a value of value
.
Alternatively, we can use the getElementsByAttributeValue
method if we want to select elements by a data attribute value without using a CSS selector. Here's an example:
index.tsx87 chars2 lines
This will find all the elements that have a data-attribute-name
attribute with a value of value
.
If we have an element with a known id
, we can also use getElementById
:
index.tsx123 chars3 lines
This finds the element with id
equal to 'element-id'
and then obtains the value of its data-attribute-name
attribute.
gistlibby LogSnag