How do I use CSS.escape() or equivalent for all browsers?

Viewed 1575

I have an id for an element that is '#*lK36*kW63*x1*t1510689412121569' or something similar in runtime.

CSS.escape works when I'm running this in Chrome and Firefox. However, it doesn't work (as the doc says) for IE or Safari or Edge.

Is there a convenient function/hack I can use to make CSS.escape(str) work in all browsers?

1 Answers

Credit to @epascarello for posting this (albeit in the comments)

You can download the polyfill from here https://github.com/mathiasbynens/CSS.escape that will allow you to use CSS.escape('#*lK36*kW63*x1*t1510689412121569') in all browsers.

Note though that this also escapes the pound/hash sign at the beginning. I'm not sure if that's part of the ID itself or that's the selector for the ID that follows, but it's important to keep that in mind.

Related