The CSS.escape()
static method returns a string containing the escaped string passed as parameter, mostly for use as part of a CSS selector.
CSS.escape(".foo#bar")
CSS.escape("()[]{}")
CSS.escape('--a')
CSS.escape(0)
CSS.escape('\0')
To escape a string for use as part of a selector, the escape()
method can be used:
var element = document.querySelector('#' + CSS.escape(id) + ' > img');
The escape()
method can also be used for escaping strings, although it escapes characters that don't strictly need to be escaped:
var element = document.querySelector('a[href="#' + CSS.escape(fragment) + '"]');