Picture of Mehul Kar

Mehul Kar

Link to Twitter profile Image of envelope to indicate email link

Apr 11, 2018

Auto Closing Attribute Selectors?

programming frontend

I discovered something weird the other day. It looks like in Chrome and Firefox, attribute selectors will automatically be closed. Consider this example:

// <div some-attr>hello</div>
const selector = '[some-attr]';

Notice the opening and closing square brackets that form the valid selector. Now see this selector:

// <div some-attr>hello</div>
const selector = '[some-attr';

Notice the missing closing square bracket.

In Chrome 66 and Firefox 59, this selector will still work. But in Safari, it does not work. I was even able to get selectors with values to work without closing quotation marks:

// <div some-attr-with-value="1">hello</div>
const goodSelector = '[some-attr-with-value="1"]';
const badSelector = '[some-attr-with-value="1';

document.querySelector(goodSelector); // finds the element
document.querySelector(badSelector); // finds the element in Chrome and Firefox, but not Safari

This is super strange behavior and I have not tracked down why it is the way it is. If anyone knows, please twitter me!

Here’s an interactive example on JSFiddle: https://jsfiddle.net/mehulkar/1mobjpre/22/

Want to talk about this blag? Email me or send me a toot @mehulkar!