0

In my Rails app, I have a Post model.

I am using coffeescript to make the post form more dynamic.

In posts.coffee, I have:

$(document).ready ->
  text_max = 140
  $('#character_count').html text_max + ' characters remaining'
  $('#post_short_copy').keyup ->
    text_length = $('#post_short_copy').val().length
    text_remaining = text_max - text_length
    $('#character_count').html text_remaining + ' characters remaining'
    return
  return

This works really well.

Now, I need to insert an if statement inside this code, to only execute $('#character_count').html text_max + ' characters remaining' if $('#post_short_copy').is(':empty').

I have tried the following code:

$(document).ready ->
  text_max = 140
  if $('#post_short_copy').is(':empty') {
    $('#character_count').html text_max + ' characters remaining'
  }
  $('#post_short_copy').keyup ->
    text_length = $('#post_short_copy').val().length
    text_remaining = text_max - text_length
    $('#character_count').html text_remaining + ' characters remaining'
    return
  return

but this returns an error:

SyntaxError: [stdin]:7:3: unexpected if

Any idea how to properly implement an if statement in coffeescript?

1 Answer 1

1

This was probably an indention error, similar to this one:

Unexpected 'INDENT' in CoffeeScript Example Code

Consider checking tabs and spaces in your editor.

Sign up to request clarification or add additional context in comments.

1 Comment

You are correct. Just replaced the tab with two spaces and it worked. Thanks a lot.

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.