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.
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'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With