The escapeSelector()
method is a built-in method in jQuery
, and is used to escape CSS meta-characters.
jQuery
uses some sort of selector for selecting DOM elements. If any selector contains special characters like #
or .
, then to use such a selector we need to use the escapeSelector()
method.
$.escapeSelector(selector);
This method accepts the following parameters:
selector
: This is the selector that needs to be escaped.Let’s look at an example of the escapeSelector()
method in the code snippet below:
In the HTML tab:
div
content.p
elements.Note: We add the
.line
class to every alternativep
.
Highlight
.jQuery
script.In the JavaScript tab:
click()
method on the Highlight
button.escapeSelector()
method to select all the elements with class .line
. Also, we use the toggleClass()
method to toggle class highlight
.Highlight
button is clicked, the escapeSelector()
method escapes the selector .line
and all the elements with this selector are selected.