selectors-api for data attributes

Posted by MJ on Stack Overflow See other posts from Stack Overflow or by MJ
Published on 2010-04-19T04:56:56Z Indexed on 2010/04/19 5:03 UTC
Read the original article Hit count: 333

Filed under:
|

In HTML5, CSS selectors seem to operate well with data-* attributes. For example:

<style>
div[data-foo='bar'] {
  background:#eee;
}
</style>
<div data-foo='bar'>colored</div>
<div>not colored</div>

will properly style the first . But, attempts to select such elements using the selectors-api fail. Examples:

var foos = document.querySelectorAll("div[data-foo]='bar'");

or

var foos = document.querySelectorAll("div data-foo='bar'");

in Chrome and Safari, this produces a cryptic error:

SYNTAX_ERR: DOM Exception 12

Any thoughts on how to use the selectors-api to properly select elements on the basis of data-* attributes?

© Stack Overflow or respective owner

Related posts about selectors-api

Related posts about html5