Click binding event in jquery coffeescript

I work on rails 3.2 and use coffeescript too .. I have doubts about using jQuery mobile inside my application I have html like

<a href="#" data-item=12 data-status=true class="follow">
  <span class="ui-btn-inner">
    <span class="ui-btn-text">
      Follow
    </span>
  </span>
</a>

In my coffeescript, I am trying to write a function by clicking this Follow, I am trying to send an ajax call.

when I wrote how

  $("a.follow").unbind('click').bind 'click', (event, data) ->
      event.stopPropagation()
      clickedEl = $(event.target)
      console.log(clickedEl)

Console.log occasionally prints a range with the class "ui-btn-text" with "ui-btn-inner".

But in my coffeescript function, I need to actually use the attributes from the tag. How to do it

+5
source share
2 answers

You can use event.currentTargetinstead event.target:

The current DOM element in the bubbling phase of events.

Demo: http://jsfiddle.net/ambiguous/gdzUC/

event.delegateTarget:

, jQuery .

: http://jsfiddle.net/ambiguous/UhBWM/

closest:

clickedEl = $(this).closest('a')

: http://jsfiddle.net/ambiguous/2LHp3/

event.target:

DOM, .

, , . currentTarget delegateTarget . closest, , , , DOM , , .

+10

jQuery, , , .

...
  clickedEl = $(event.target)
  parentLink = clickedEl.parentsUntil $("a.follow")
  console.log(parentLink)
-1

All Articles