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>&#10084;</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

Popular posts from this blog

java - Run a .jar on Heroku -

java - Jtable duplicate Rows -

validation - How to pass paramaters like unix into windows batch file -