Meltemi
Meltemi

Reputation: 38359

Convert jQuery .click into .live 'click' in CoffeeScript

This works:

$('#products.table tr').click ->
  $(@).toggleClass('row_selected')

This doesn't:

$('#products.table tr').live 'click', (event) ->
  $(@).toggleClass('row_selected')

Error in browser's console:

[Error] TypeError: 'undefined' is not a function (evaluating '$('#products.table tr').live')
    ready (products.js, line 10)
    fire (jquery.js, line 3049)
    fireWith (jquery.js, line 3161)
    ready (jquery.js, line 434)
    completed (jquery.js, line 105)

What am I missing?

Upvotes: 1

Views: 3487

Answers (1)

vee
vee

Reputation: 38645

live has been deprecated as of jQuery version 1.7, and Rails 3's default jQuery version is 1.9. You need to use on instead:

Try:

$(document).on 'click', '#products.table tr', (event) ->
  if ( $(@).hasClass('row_selected') )
    $(@).removeClass('row_selected')
  else
    $(@).addClass('row_selected')

or, you could use the toggleClass method as suggested in the comments as:

$(document).on 'click', '#products.table tr', (event) ->
  $(@).toggleClass('row_selected')

Upvotes: 7

Related Questions