javascript - Getting correct element from click event -
i have dom tree this:
<div class="like-bttn" data-answer-id="{{answer.id}}"> <div class="count>{{ answer.like_score }}</div> <div class="heart>❤</div> </div>
i have javascript code make ajax request when like-bttn clicked:
$(".like-bttn").click(function(ev) { var id = $(ev.target).attr('data-answer-id'); ... id ... })
but when .heart clicked code not work, becaouse ev.target .heart , not have 'data-answer-id'
what correct approach @ stiuation?
use $(this)
, not event target :
$(".like-bttn").click(function(ev) { var id = $(this).attr('data-answer-id'); ... id ... })
and, neal pointed, it's cleaner use data
:
var id = $(this).data('answerid');
Comments
Post a Comment