Reputation: 529
How can I get the text()
value of an anchor tag I have clicked from a set of anchor tags. This code below gets all the text value of the anchor tags:
$('#search-helper-container, .search-match a').on('click',function(e){
e.preventDefault();
var test = $(this).find('a').text();
console.log(test);
});
How can I modify if so that I get only the text value of the anchor tag I click? Fiddle
Upvotes: 1
Views: 264
Reputation: 388316
You need to bind the click handler to the anchor elements with class search-match
which are inside the container #search-helper-container
. So you need to change the selector as given below then this
inside the click handler will refer to the clicked anchor element.
$('#search-helper-container a.search-match').on('click', function (e) {
e.preventDefault();
var test = $(this).text();
console.log(test);
});
Demo: Fiddle
Upvotes: 4