jQuery & jQuery UI Documentation

jQuery & jQuery UI

Attribute

The CSS specification also allows elements to be identified by their attributes. While not widely supported by browsers for the purpose of styling documents, these attribute selectors are highly useful, and jQuery allows us to employ them regardless of the browser being used.
When using any of the following attribute selectors, we should account for attributes that have multiple, space-separated values. Since these selectors see attribute values as a single string, this selector, for example, $("a[rel='nofollow']"), will select <a href="example.html" rel="nofollow">Some text</a> but not <a href="example.html">Some text</a>.

Attribute values in selector expressions must be surrounded by quotation marks.

  • double quotes inside single quotes: $('a[rel="nofollow self"]')
  • single quotes inside double quotes: $("a[rel='nofollow self']")
  • escaped single quotes inside single quotes: $('a[rel=\'nofollow self\']')
  • escaped double quotes inside double quotes: $("a[rel=\"nofollow self\"]")

The variation you choose is generally a matter of style or convenience.

Note: In jQuery 1.3 [@attr] style selectors were removed (they were previously deprecated in jQuery 1.2). Simply remove the “@” symbol from your selectors in order to make them work again.