More attribute selectors

It is also possible to select elements using just part of a value of an attribute. The terms below are defined in terms of any stringInformation appearing between quotation marks., which for our purposes is the sequence of characters making up an attribute value. Examples that we have seen before include special for an ID and odd for a class.

Term Definition
prefix A new string formed by removing zero or more characters from the end of a string.
suffix A new string formed by removing zero or more characters from the beginning of a string.
substring A new string formed by removing zero or more characters from the end of a string and zero or more characters from the beginning of a string.

Using special as our original string, sp, spec, and special are all examples of prefixes (the last one formed by removing zero characters), and al, cial, and special are all suffixes. All prefixes and suffixes are substrings. Other examples of substrings include eci and pec. The string ea is not a substring, since some of the characters that were removed were removed from the middle of the string, not from the beginning or the end.

We can use these in selectors:

  • Use tagname[attribute^=prefix] to select elements with a particular attribute and prefix of the value.
  • Use tagname[attribute$=suffix] to select elements with a particular attribute and suffix of the value.
  • Use tagname[attribute*=substring] to select elements with a particular attribute and a substring of the value.