Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
});
like image 860
peter Avatar asked Oct 21 '25 06:10

peter


2 Answers

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:-

  • Element Selector (“element”)
  • Class Selector (“.class”)
like image 97
palaѕн Avatar answered Oct 23 '25 19:10

palaѕн


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

$('.cls').click(function(e) {
  e.preventDefault();
  console.log($(this).attr('href'));
});
like image 25
Rajaprabhu Aravindasamy Avatar answered Oct 23 '25 21:10

Rajaprabhu Aravindasamy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!