Help with a CSS selector that targets all elements that have "aria-*" attributes

Is it possible to target all elements that have an attribute with a specific prefix like "aria-"? I want to select all aria attributes like "aria-label", "aria-labelledby", "aria-level" and so on...
15 Replies
ἔρως
ἔρως2mo ago
do you use sass? if you do, make a list with all attributes if not, do the work manually you can use :is(), :where() or repeat each selector manually
snxxwyy
snxxwyy2mo ago
you could also do this using js by doing something like:
const elements = document.querySelectorAll("body *");

elements.forEach(elem => {
attributes = Array.from(elem.attributes);
attributes.forEach(attr => {
if (attr.name.startsWith("aria")) {
console.log(`${elem.tagName} has an aria attribute.`)
}
})
})
const elements = document.querySelectorAll("body *");

elements.forEach(elem => {
attributes = Array.from(elem.attributes);
attributes.forEach(attr => {
if (attr.name.startsWith("aria")) {
console.log(`${elem.tagName} has an aria attribute.`)
}
})
})
ἔρως
ἔρως2mo ago
unless it is to use in css, for styles
snxxwyy
snxxwyy2mo ago
yeah
clevermissfox
clevermissfox2mo ago
*[aria-*] {
/* Your styles here */
}
*[aria-*] {
/* Your styles here */
}
ἔρως
ἔρως2mo ago
No description
snxxwyy
snxxwyy2mo ago
you can only do that with values of the attribute i believe, not the name. e.g.
[data-test^="aria"] {
/* Your styles here */
}
[data-test^="aria"] {
/* Your styles here */
}
meaning it selects all elements that have the attribute of data-test with a value that starts with aria i think to actually test if the name includes a substring you have to use js
ἔρως
ἔρως2mo ago
the challenge is to select anything that has an attribute starting with aria- it's the name of the attribute, not the value
snxxwyy
snxxwyy2mo ago
i know i was saying it in response to the error you posted
ἔρως
ἔρως2mo ago
the error was to show it doesnt work would be awesome if it did
snxxwyy
snxxwyy2mo ago
yeah, i was just explaining why it didn't to benefit op and clevermissfox
ἔρως
ἔρως2mo ago
oh, i though you misunderstood what i sent
snxxwyy
snxxwyy2mo ago
i could have referred to the context in my message a bit better, that's my bad.
ἔρως
ἔρως2mo ago
no, it's my bad too i should pay more attention
snxxwyy
snxxwyy2mo ago
it's all good, we got it in the end
Want results from more Discord servers?
Add your server