Universal CSS selector to match any and all HTML data-* attributes [duplicate]
Asked Answered
N

1

5

Is it possible to match all nodes that have a data-* attribute using only CSS?

Here are examples of attributes I would like to match:

data-scope
data-sessionlink
data-visibility-tracking

I could do

*[data-scope] *[data-sessionlink] *[data-visibility-tracking]

but I am looking for something more compact. Furthermore, I don't know all possible data-* attributes I might encounter in my application.

Nealson answered 26/4, 2016 at 19:43 Comment(5)
Are these attributes or attribute values (e.g. Class="data-scope")Cureall
These are attributes.Nealson
Dang. There's a wildcard search for attribute values starting with a string, but not, as far as I know, attributes.Cureall
Yes, I'm familiar with the wildcard attribute value selector, and I am not holding out much hope that what I would like to do is possible, but you never know until you ask others.Nealson
I'm not certain this is possible without the use of javascriptHound
N
3

It is currenly impossible to use wildcard masks to select elements by an attribute-name part.

There is a recent thread in the [email protected] mailing list, where Simon Pieters from Opera has proposed a nice possible syntax that has got some acceptance in the thread, so there is a chance that it will become standard somewhen in the future:

x-admin-* { ... }
[data-my-*] { ... }
Nitty answered 26/4, 2016 at 23:11 Comment(3)
Thanks, Marat! I hope that one day we will be able to do wildcard selection on attribute names.Nealson
Not the greatest idea though - won't allow you to do [data-my-*='exact match'] because *= clashes with an existing operatorNike
@Nike Good point. But this could probably be resolved by wrapping the attribute name itself in quotes: ["data-my-*" = "exact match"]Nitty

© 2022 - 2024 — McMap. All rights reserved.