CoffeeScript Cafe

CoffeeScript tutorials, examples, and news

jQuery Event Binding in CoffeeScript

Let’s say you want to write a small bit of CoffeeScript to do something simple like show an ajax spinner image when the user submits a given form. The powers of jQuery and CoffeeScript combine to give us:

CoffeeScript

Compiled JavaScript

Not too fancy right?

Key things to notice in this example

  • -> starts a new function
  • whitespace delineates the functions
  • no semicolons

View this jQuery binding example on coffeescript.org.

Update: Improved CoffeeScript example from Nihad Abbasov

Nihad Abbasov (Narkoz) shared an improved version of the CoffeeScript binding. The code structure can be chained into a clean line of CoffeeScript and @ can replace this. Very nice!

Web Analytics