-1

I have my <ima> which contains the data, data-commentId, and I try to use a function to get the data, but it's not working

JS

function getsome(){
    var comment_id=$(this).attr('data-commentId');
    alert(comment_id);
}

HTML

echo '<img onclick="getsome()" class="c_like_icon" data-commentId="'.$reply_id.'"  src="img/thumb_icon.png"  height="18" width="18">';
BeNdErR
  • 17,471
  • 21
  • 72
  • 103
kesong
  • 309
  • 1
  • 5
  • 13

1 Answers1

2

If you want this inside the function to refer to the element, you have to use .call and pass this from the inline event handler:

onclick="getsome.call(this)"

Of course, since you are using jQuery, there are far better ways to bind event handlers, and I just answered a question why using inline event handlers should be avoided.

Community
  • 1
  • 1
Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143