Question

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?

Was it helpful?

Solution

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top