2

I'm wondering if its possible to addClass to a link, that has the same href as the document.href?

I tried, but without any luck.

if ($("a").attr("href") == document.location.href) {
    $(this).addClass("active");
}

Isn't this possible??

curly_brackets
  • 5,491
  • 15
  • 58
  • 102

3 Answers3

6
$("a").filter(function() {
    return this.href === document.location.href;
}).addClass("active");

Should work.

Ry-
  • 218,210
  • 55
  • 464
  • 476
1
$("a[href=" + document.location.href + "]").addClass("active");

(not tested)

Julien
  • 833
  • 8
  • 20
0

Have you tried window.location.href instead of document.location.href?

SickHippie
  • 1,382
  • 1
  • 8
  • 20