Joe Masilotti
Joe Masilotti

Reputation: 17018

Given a URL how can I find the anchor HTML anchor tag?

I am working with a JavaScript framework that performs a callback when a link is clicked. The method includes the full path of the URL that was selected.

Given the URL, is there a JavaScript (or jQuery) selector to target the HTML anchor tag that was clicked?

Upvotes: 2

Views: 399

Answers (2)

7urkm3n
7urkm3n

Reputation: 6321

This one should print, specific clicked tag...

$('a').on('click', function(){
   var attr = $(this).attr('href');
   console.log(attr);
})

Upvotes: 0

Victory
Victory

Reputation: 5890

Using jQuery you can use the attribute selector, which goes like [attribute='attribute value']

$('a').click(function () {
  var $this = $(this);
  var href = $this.attr('href');
  var selector = "a[href='" + href + "']";
  console.log(this, $(selector));
});

Upvotes: 1

Related Questions