JMon
JMon

Reputation: 3447

get the text inside an anchor after clicking on the image

I have several links similar to the following HTML, however different text :-

<a class="rpLink " href="#">
   <span class="rpOut">
   <span class="rpExpandHandle"></span>
   <img class="rpImage" src="Images/favourite.png">
    <span class="rpText">Baskets with Product </span>
   </span>
</a>

I am trying to get the text (inside the rpText) when the user clicks on the favourite image.

I have the following JQuery up until now :-

        $('.rpItem').on("click", "img", function (e) {
        var text = $(this).find(".rpLink").text();
        e.preventDefault();
        var args = {
            reportName: e
        };
        alert($(this).attr('href'));
        $.ajax({
            type: "POST",
            url: "Default.aspx/AddToFavourites",
            data: JSON.stringify(args),
            contentType: "application/json;charset=utf-8;",
            success: function () {
            },
            error: function () {
                alert("Fail");
            }
        });
    });

However I cannot manage to get the text.

Upvotes: 0

Views: 154

Answers (2)

bipen
bipen

Reputation: 36551

If the click event is for the favourite img then you can use siblings().

Try this:

 $('.rpItem').on("click", "img", function (e) {
    $(this).siblings('span.rpText').text();
    ....

Upvotes: 1

palaѕн
palaѕн

Reputation: 73966

Try this:

var text = $(this).find(".rpText").text();

Upvotes: 0

Related Questions