2

I'm trying to write some coffeescript to give me back the link that I clicked and I'm having trouble.

The issue is that the element I'm getting back is the selector that starts this chain but that's not what I'm looking for.

$(document).ready ->
  $("#js-exercises-links").bind "cocoon:after-insert", (e, inserted_item) ->
    $("#js-exercise-links a.add_fields").data("association-insertion-node", '#js-outline-fields')
    $("select").chosen()

    $(".js-show-set-duration").click (e)=>
      e.preventDefault()
      console.info this

In this example, when I look at my console I get back <div id='js-exercises-links'> what I really want is the current element I clicked on which would be an instance of <div class='js-show-set-duration'>

What am I doing wrong? How do I get $(this) to reference the element clicked on?

1 Answer 1

6

I believe you want the skinny arrow and not the fat arrow, its hard for me to test this without the html though.

$(document).ready ->
  $("#js-exercises-links").bind "cocoon:after-insert", (e, inserted_item) ->
    $("#js-exercise-links a.add_fields").data("association-insertion-node", '#js-outline-fields')
    $("select").chosen()

    $(".js-show-set-duration").click (e)->
      e.preventDefault()
      console.info this
Sign up to request clarification or add additional context in comments.

5 Comments

Or you could dig it out of e.
When I do that I get the html elements back in the console but not the actual element in the dom. I want the element so I can run more jquery methods on it.
Dig it out of e with e.target. What do you the actual element?
I'm not able to run any jQuery methods on e.target or this with the -> e.g, $(this).css 'background-color', 'red' doesn't work, nor does $(e.target).css 'background-color', 'red'. It's not modifying that specific element. What I want to do is do $(this).parent but then that returns a jQuery function instead of the neighboring element...
I figured it out. Chrome decided to update the way that it renders logged objects in the console... had to do object[0] to get the old return syntax back. (So hover highlights the object on the page).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.