-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
  • 16,807
  • 17
  • 66
  • 100
kesong
  • 189
  • 1
  • 3
  • 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
  • 756,363
  • 169
  • 1,062
  • 1,111