peter
peter

Reputation: 4411

get url of clicked href

How can I get url of clicked anchor text everywhere on the page. I tried this but it only works for "cls" class but not for any anchor.

$('.cls').click(function() { 
//here I need to get href url
});

Upvotes: 4

Views: 2220

Answers (3)

palaѕн
palaѕн

Reputation: 73896

For any anchor in your page you can simply do

$('a').click(function () {
    alert($(this).attr('href'));
});

This will give you url of clicked anchor text everywhere on the page.

For more details checkout:-

Upvotes: 5

Pawan
Pawan

Reputation: 1744

You can use attr() as below:

$(function(){
    $('a').click(function(){
    alert($(this).attr('href'));
});

Upvotes: 1

Rajaprabhu Aravindasamy
Rajaprabhu Aravindasamy

Reputation: 67207

Try to use .attr('attributeName') to retrieve the clicked element's attribute,

$('.cls').click(function(e) {
  e.preventDefault();
  console.log($(this).attr('href'));
});

Upvotes: 1

Related Questions