0

I have this image in my view, which I would like to link to a javascript function when clicked (coffeescript to be precise:

<%= image_tag("minus_un.png", {:id =>"entree-show-minus-2", :title => "test bouteille", :onclick => "EditCount(" + @entree.id.to_s + ")"}) %>

And in my .js.coffee file, I have this:

EditCount = (id) ->
  console.log "coin_" + id.to_s

But clicking the image gets the error message Uncaught ReferenceError: EditCount is not defined. Why is it not found? The jQuery(document).ready -> and below are correctly executed.

Also, I'm wondering if this is even the right way to do it. By doing that, I'm referencing JS functions in the view, and I'm not sure this is correct unobtrusive javascript.
The alternative would be using the $("#entree-show-minus-2").live("click", {block} ) syntax. It works, but that that means I have to retrieve the parameters needed from some elements in the DOM, and that seems quite tedious.

What is the best practice?

1 Answer 1

1

Go with your alternative approach and pass the parameters in via data attributes on the image tag.

Sign up to request clarification or add additional context in comments.

Comments

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.