Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Query selector attribute value starts with empty string fails

Can anyone explain why the following returns an empty list:

var el = document.createElement('div');
el.dataset.mydata="hello";
document.body.appendChild(el);
document.querySelectorAll('div[data-mydata^=""]');

(If you're wondering why you'd ever do this, the actual code is using a variable as the attribute value in the querySelector. The function it is part of gets called recursively, on the first run the variable is empty.)

like image 975
Dave Kennard Avatar asked May 11 '26 11:05

Dave Kennard


1 Answers

This happens because substring matching attribute selectors are dumb:

[att^=val]

Represents an element with the att attribute whose value begins with the prefix "val". If "val" is the empty string then the selector does not represent anything.

(Emphasis mine.)

All substring selectors are broken by design: When given the empty string, they return nothing (instead of matching all strings). I have no idea why, but it's what the selector specification says.

The best fix is probably to add a check to your code that says if var is empty, use 'div[data-mydata]' instead (i.e. only check for the presence of the attribute, not its value).

like image 175
melpomene Avatar answered May 14 '26 03:05

melpomene