a_miguel6687
a_miguel6687

Reputation: 529

Get text of specific anchor tag

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

Answers (1)

Arun P Johny
Arun P Johny

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

Related Questions