0

I have the following on my html:

<input id="submitbutton" type="submit" />

Further down I then have:

<script type="text/coffeescript">
            $('#submitbutton').click() ->
                alert('hello')
</script>

Yet when I click the button the alert doesn't get triggered.

8
  • Do you have the CoffeeScript compiler included so that text/coffeescript is understood? Anything in the error console? PS: you might want to look at the JavaScript version of your CoffeeScript, it doesn't say what you think it does. Commented Sep 24, 2014 at 2:23
  • Thanks, the compiler is included and working elsewhere, but there is an 'uncaught TypeError: object is not a function' Commented Sep 24, 2014 at 2:41
  • And have you looked at what $('#submitbutton').click() -> alert('hello') gets turned into? Commented Sep 24, 2014 at 3:00
  • Looks like it gets turned into $('#submitbutton').click()(function() { return alert('hello'); }); Is there something here that should be obvious? Sorry if so, pretty new to js/coffeescript. I'm not worried about the alert itself, that's just a placeholder for the function to write later, the issue is that I can't get a function to run from the button click Commented Sep 24, 2014 at 3:31
  • 3
    $('#submitbutton').click -> alert('hello') should work. Commented Sep 24, 2014 at 4:10

1 Answer 1

1

I believe you have to wrap that in another function:

<script type="text/coffeescript">
  $ ->
    $('#submitbutton').click() ->
      alert('hello')
</script>

I think (though I am not sure) this is to ensure that the document is fully loaded before that function is bound to the button (so that the button actually exists when the binding takes place)

Based on this question: How to register Jquery click event with coffeescript on rails 3.1

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

Comments

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.