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

11,146

Solution 1

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

Solution 2

I believe the problem here is a lack of parentheses:

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

Edit: @Jeremy beat me to it

Share:
11,146
obo
Author by

obo

Updated on July 17, 2022

Comments

  • obo
    obo almost 2 years

    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.
    
  • obo
    obo over 12 years
    Many thanks! I feel so unconfortable fot that silly question. Well, I hope this will be helpful for other developers...