I'm considering one selection statement that would target one of many css class names in a single class attribute value based on a string prefix.
For example, I want any detail-
prefixed class names to get targeted from the following sample links.
<a href="eg.html" class="detail-1 pinkify another">
<a href="eg.html" class="something detail-55 minded">
<a href="eg.html" class="swing narrow detail-Z">
<a href="eg.html" class="swing narrow detail-Z detail-88 detail-A">
It's reminiscent of how [class|="detail"]
prefix selector works on a scalar attribute value, and also of .hasClass(className)
, but my question needs both concepts applied simultaneously.
Note: The detail-
prefix won't necessarily be the first class name of the bunch.
try wildcard selector
http://www.emadibrahim.com/2009/07/25/jquery-wildcard-selector/
Because of the way the
class
attribute is designed, you'll need to make use of at least two other attribute selectors (notice the whitespace in[class*=" detail-"]
):This selects
<a>
elements with aclass
attribute thatdetail-
, ordetail-
. Class names are separated by whitespace per the HTML spec, hence the significant space character.If you'd like to turn this into a custom selector expression, you can do this:
Then select it like this:
Or if you'd like to place this in a plugin:
Then call it like this:
You can add your own selectors, in your case you could add a regular expression selector for attributes:
Then you could do something like this:
http://jsfiddle.net/ambiguous/ArtRS/1/
I came across that article somewhere on SO but I can't remember where.
You could also write a small plugin that uses
.map()
to walk through the classes.And then: