Patrick Burns
Patrick Burns

Reputation: 1833

jQuery selector with slashes

i have html

<a href="#" data-ext="sql" rel="\dir\file.sql" id="file">file.sql</a>

And i want find this a#file among others by rel, so

$("#file[rel='\dir\file.sql']")

not works, because \ removing (why?)

var some_var = "/s/s/s/\a\a\a";
// undefined
some_var
// "/s/s/s/aaa"

So, how i can select item(items) by rel with \ in it?

And this is not all want to work

$("#file[rel='\\dir\\file.sql']")
$("#file[rel='/\dir/\file.sql']")  // ^^

Example http://jsfiddle.net/fUNLV/1/ not works :(

Upvotes: 0

Views: 549

Answers (3)

antyrat
antyrat

Reputation: 27765

You need to use double backslashed for this:

$("#file[rel='\\dir\\file.sql']")

This happens because just one \ slash will try to escape next symbol and double slashes will escape first slash.

UPDATE

Seems you need to use \\\\:

$("#file[rel='\\\\dir\\\\file.sql']")

jsFiddle demo

Upvotes: 1

Jason P
Jason P

Reputation: 27012

The backslash is an escape character. To escape the escape character, you can double it up:

Edit - In this case, the escaping needs to be double-escaped:

http://jsfiddle.net/FSqZe/

var $el = $('#file[rel="\\\\dir\\\\file.sql"]');

Upvotes: 3

Kevin B
Kevin B

Reputation: 95028

You need to escape both slashes, not just 1.

$("#file[rel='\\\\dir\\\\file.sql']") // that's 4 slashes in each case, 8 total

http://jsfiddle.net/Tentonaxe/fUNLV/2/

This is because First, this string is passed to jQuery, at which point it would become

"#file[rel='\\dir\\file.sql']"

then when it gets passed again internally to document.querySelectorAll(), the slashes are correctly escaped for the css selector engine.

http://jsfiddle.net/Tentonaxe/fUNLV/4/

Upvotes: 1

Related Questions