Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

selectors-api for data attributes [closed]

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?

like image 465
dTRN Avatar asked Jul 28 '26 18:07

dTRN


1 Answers

The syntax for attribute selectors is [att=val] so you want div[data-foo='bar'].

like image 150
Anne Avatar answered Jul 30 '26 09:07

Anne



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!