1

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?

1 Answer 1

7

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')
Sign up to request clarification or add additional context in comments.

2 Comments

Might as well switch to toggleClass while you're there: $(@).toggleClass('row_selected').
This wouldn't work: $('#products.table tr').on 'click', (event) -> but what you have now starting with $(document).on… works great! Would love an explanation from someone as to why if/when they get a chance...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.