Mo.
Mo.

Reputation: 27445

Jquery get href value

I need to get value from a href button. Here I have just developed a code which show all href value. Actually I need to get value from where I click.

here is my code

$(document).ready(function() {
  $("a.me").click(function(){
    var number = $('a').text();

    alert(number);

  });
}); 

Upvotes: 0

Views: 8505

Answers (6)

joidegn
joidegn

Reputation: 1068

one of the answers above will work or you could use

...
$("a.me").click(function(e){
    var number = $(e.target).text();
    alert(number);
});

Upvotes: 0

Bisa
Bisa

Reputation: 154

In jQuery you can use $(this) to reference the element that was clicked (or otherwise used)

Like so:

$("a.me").click(function(){
    var number = $(this).text();
    alert(number);
});

Upvotes: 1

Shyju
Shyju

Reputation: 218722

$(document).ready(function() {
  $("a.me").click(function(){
      var target=$(this).attr("href");
      alert(target);
  });
});

Upvotes: 0

Curtis
Curtis

Reputation: 103348

var number = $(this).attr('href');

Upvotes: 0

freakish
freakish

Reputation: 56467

$("a.me").click(function(){
    var number = $(this).attr('href');
    alert(number);
});

Upvotes: 4

Nicola Peluchetti
Nicola Peluchetti

Reputation: 76870

You should do

$(document).ready(function() {
  $("a.me").click(function(){
    //It's not clear if you want the href attribute or the text() of the <a>
    //this gives you the href. if you need the text $(this).text();
    var number = this.href;
    alert(number);

  });
}); 

Upvotes: 5

Related Questions