Reputation: 4411
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
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
Reputation: 1744
You can use attr() as below:
$(function(){
$('a').click(function(){
alert($(this).attr('href'));
});
Upvotes: 1
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