1

I'm trying to write a jQuery selector to find links to files contained in a documents folder. There are several documents folders in various places on the site. A sample URL to match is

http://example.com/magazine/volume5/documents/magazine2010_11-8_final.pdf

This works:

$("a[href*='documents']")

But for completeness, I want to match the slashes on either side. I see that to include a slash in a selector, I need to prefix it with two backslashes, like this: \\/. But that's working only for the trailing slash, not the leading slash.

This works:

$("a[href*='documents\\/']")

But this does not:

$("a[href*='\\/documents\\/']")

What am I doing wrong?

Update On jsFiddle (http://jsfiddle.net/jA3AJ/), it works, but not on my site (using the same browser, Firefox 4.0). My full code snippet is

<script type="text/javascript">
    // Instrument download links for GA
    $(document).ready(function () {
        $("a[href*='\\/documents\\/']").click(function () {
            alert("Download: " + this.pathname);
            // if (typeof _gaq !== 'undefined') { _gaq.push(["_trackEvent", "Files", "Download", this.pathname]); }
        });
    });
</script>

According to the answer to this How do I get jQuery to select elements with a . (period) in their ID?, I need to use two backslashes. The jsFiddle works for me either way. In fact, it works with no backslashes: $("a[href*='/documents/'])"

Updated I'm using jQuery 1.6.4. I updated the jsFiddle: http://jsfiddle.net/jA3AJ/1/ and there, it works fine.

Community
  • 1
  • 1
Carl Raymond
  • 4,429
  • 2
  • 25
  • 39

2 Answers2

2

Its works for me. Use $("a[href*='\/documents\/']"). If you want to escape a forward slash just put one escape character(\) in front of it. You don't need 2 escape characters.

Wroking Demo

ShankarSangoli
  • 69,612
  • 13
  • 93
  • 124
2

Reverse your single and double quotes

$('a[href*="\/documents\/"]')
Kyle Macey
  • 8,074
  • 2
  • 38
  • 78