To find an element on a page by a custom attribute using JavaScript, you can use the querySelectorAll
method and pass in the attribute selector as a string. Here's an example:
index.tsx90 chars2 lines
In this example, we are using the attribute selector [data-custom-attribute="your-custom-value"]
to find any element with an attribute named data-custom-attribute
with a value of your-custom-value
. The querySelectorAll
method returns a NodeList of all matching elements, which you can then manipulate or access as needed.
gistlibby LogSnag