Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to hide and show a div with coffeescript - Rails 3.1

I am trying to make a similar behavior as the comment link on a question in Stack Overflow. A click should hide the div containing the "Add comment" link and show another div containing the form for a new comment.

How is it possible to do that with coffeescript ?

I am trying the following that does not work :

jQuery ->

  hide_comment_link = () ->
    $('#add_comment_link').hide
  hide_comment = () ->
    $('#add_comment').hide
  show_comment = () ->
    $('#add_comment').show

  $('#add_comment_link').click ->
    hide_comment_link
    show_comment
    false

View is :

#add_comment_link
  #{link_to "Add a comment"}
#add_comment
  Add a comment in this div.
like image 368
obo Avatar asked Nov 19 '11 19:11

obo


1 Answers

Unlike Ruby, CoffeeScript doesn't allow you to omit parentheses from function/method calls if there are no arguments. You need to add them:

$('#add_comment_link').hide()
hide_comment_link()

The do keyword is another option:

do $('#add_comment_link').hide
do hide_comment_link
like image 95
Jeremy Avatar answered Oct 23 '22 13:10

Jeremy