I have this line of code in my html
<a href="#" onclick="editfuntion(2)">
And My Coffescript
editfunction = (id) ->
console.log id
return
Why is it it returns ? Uncaught ReferenceError: editfunction is not defined at HTMLAnchorElement.onclick
Coffeescript will wrap all files in an anonymous function which is immediately executed. This means all variables you define are locally scoped unless explicitly placed in the global namespace (in browsers it is the
window
object, while in NodeJS it'sglobal
).From coffeescript.org (talking about using Coffeescript in a browser, but it also applies to using the Coffeescript compiled to JS ):
Try changing your Coffeescript to:
This will expose the function globally on the
window
object so that it can be used within anonclick
handler.this will works :)