Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Translate this jquery into coffee script?

I'm unsure how to structure the following. This works fine:

  $('.hover li').on 'hover', (ev) ->
    $(this).addClass('active')

I know I can use 'toggle' instead of 'addClass', but for other reasons I need a handler out function passed. So I tried this:

  $('.element').on 'hover', (ev)
    -> $(this).addClass('active'),
    -> $(this).removeClass('active')

This returns an error - 'unexpected ,'. I've tried other variations, and most of the examples I've found online do not use the .on 'hover' (ev) -> format.

like image 992
Damien Roche Avatar asked Jun 10 '12 15:06

Damien Roche


1 Answers

You cannot use on() for this if you'd like to attach both event handlers at a time.

You need to use hover():

$('.element').hover(
  (ev) -> $(this).addClass 'active'
  (ev) -> $(this).removeClass 'active'
)

or even better, utilizing toggleClass()

$('.element').hover (ev) -> $(this).toggleClass 'active'
like image 74
Niko Avatar answered Oct 18 '22 16:10

Niko