karthik
karthik

Reputation: 13

jquery find anchor by rel tag

I have an anchor tags which look like this

<a rel="C:\dev/ACME.Console/3rdPartyBinaries/" href="#">3rdPartyBinaries</a>

which may be nested in li or ul tags

and I am trying to set the background color of those anchor tags when clicked. I am right now doing that with the following function.

function (file) {        
    $('a[rel="' + file + '"]').css("background", "#eee");

So when ever an anchor element is clicked the above function is hit and the file parameter is passed as the anchors rel attribute. But the background is not being set with the above method.

Any help would be greatly appreaciated

Upvotes: 1

Views: 1186

Answers (1)

alex
alex

Reputation: 490233

You need to double escape the \ in your string.

file('C:\\\\dev/ACME.Console/3rdPartyBinaries/')

At the moment, your \ is escaping the d, which isn't a special escape sequence so is just returning d. You need to escape the escape character and then escape it again for the selector string.

jsFiddle.

Instead of remembering to escape it twice, you could handle the escape sequence for the selector inside of the function.

file = file.replace(/\\/g, '\\\\');

jsFiddle.

Upvotes: 3

Related Questions