Search code examples
javascriptjqueryspecial-characters

Read special character in jQuery


I'm working with font awesome icons and use them as:

#header > nav > a[href="#menu"]::before {
    content: '\f0c9';
}

And I want to write some code which will change this icon to another after click:

var style = $('<style />').appendTo('head');
$("#three_rows").click(function() {
    style.text("#header>nav>a[href='#menu']:before{content:'\f00d'}");
});

This code works good if I use some simple text in content property, but when I tried to put another fa-icon shortcode it did nothing.

As I understand the problem is in "\" character.

How can I use it "\" in jQuery?


Solution

  • You need to escape the \ by using another \:

    var style = $('<style />').appendTo('head');
    $("#three_rows").click(function() {
        style.text("#header>nav>a[href='#menu']:before{content:'\\f00d'}");
    });