jQuery.escapeSelector()

Escapes any character that has a special meaning in a CSS selector.

jQuery.escapeSelector(selector)🡢 Selector

selector SelectorA string containing a selector expression to escape.

This method is useful for situations where a class name or an ID contains characters that have a special meaning in CSS, such as the dot or the semicolon.

The method is essentially a shim for the CSS Working Group's CSS.escape() method. The main difference is that $.escapeSelector() can be reliably used in all of jQuery's supported browsers.

Escape an ID containing a hash.

HTML
$.escapeSelector("#target"); // "\#target"
DEMO

Select all the elements having a class name of .box inside a div.

HTML
$("div").find("." + $.escapeSelector(".box"));
DEMO

Looking for a Web Developer?

👋

Hi! I'm Basti, author of this site. If you are looking for a web developer with 15+ years of experience, holla at me!

Be it the good 'ol jQuery, vanilla JS or modern frameworks like Vue and Svelte, front- or backend, I can help you.

Just write me at jobs@jqapi.com :)