2

How do I get get the value after # in the link when that link is clicked?

$(".myLink").click(function(){ 
    ???
}

<a href="myPage.php?a=asdasdasd#value" class="myLink">link</a>
Eli
  • 14,779
  • 5
  • 59
  • 77
santa
  • 12,234
  • 49
  • 155
  • 255

5 Answers5

7
$('a.myLink').click(function() {
  alert(this.hash);
  return false;
});
js1568
  • 7,012
  • 2
  • 27
  • 47
4
$(".myLink").click(function(evt){ 
    var arr = $(this).attr('href').split('#');
    alert(arr[1]);
    evt.preventDefault();
});
ezmilhouse
  • 8,933
  • 7
  • 29
  • 38
2

In browsers supporting HTML5 you can use this.hash and then take .substring(1) to remove the hash itself from that string.

This page suggests that support is pretty widespread anyway, even though its formal specification is pretty recent.

Alnitak
  • 334,560
  • 70
  • 407
  • 495
1
$(".myLink").click(function(){ 
    var link=$(".myLink").attr('href');
    pos=link.indexOf("#", 0);
    value=link.substring(pos,link.length);
}
MalTec
  • 1,350
  • 2
  • 14
  • 33
0
var href = $(this).attr("href");
var hash = href.indexOf("#") > 0 
    ? href.substring(href.indexOf("#") + 1) 
    : "";
hunter
  • 62,308
  • 19
  • 113
  • 113