javascriptjqueryasp.netasp.net-mvckendo-listview

Get text in <a> tag on image button Click


<td style="float:right;padding-top:5px">
   <a class="testAccId">#:AccountID#</a>                                                                   
   <img id="imgbtn" class="clsimg" onclick="history(event)" src="../Content/images/history.png" />                                 
</td>

I have an anchor tag and an image button, on clicking the image button I need the text in that anchor tag, and that 'td' is in loop, so if I have 2 items, when clicking one the img in 1st tile, I should get the text in from 1 st tile.

I have tried the following options.

 function history(e) {
        var a = $(this).closest('td').next().find('.testAccId').text();
        alert(a);
    }


 function history(e) {

    var a= ($(".imgbtn").closest(".testAccId")).text();
    alert(a);
}

function history(e) {
        var a = $(e.currentTarget).closest('td').next().find('.testAccId').text();
        alert(a);
    }

In the alert I am getting empty or, object object.

Please help me with this.


Solution

  • Maybe you can try something like, it's a bit hard to tell w/o an example but this should work as long as the structure is consistent

    function history(e) {
     var a = ($(e.target).prev().html());
      alert(a)
    }
    

    I updated the code here as well, should be functional now ^^