0

I'm having strange behaviour where within the same file a change event is working but a click event isn't. I understand I may have not posted enough code, but I just want to see if anyone knows why on event may work but another will not. Here is my code:

class AddBTS
  constructor: () ->

    $('#a').on 'change', (evt) => @a evt
    $('#b').on 'change', (evt) => @b evt
    $('#c').on 'click', (evt) => @c


  a: (evt) =>
    console.log 'a works'

  b: (evt) =>
    console.log 'b works'

  c: () =>
    console.log 'c works'

The html it refers to:

<input type="file" id="a">
<input type="file" id="b">
<button id="c">OK</button>

The events work fine on a and b, but the click event doesn't work on c.

My compiled JS is executed after the DOM loads.

Could anyone give me some pointers on what may cause this and I will try it out.

Interestingly, when I double click on c I get the following error:

Error in event handler for 'undefined': IndexSizeError: DOM Exception 1 Error: Index or size was negative, or greater than the allowed value.

2 Answers 2

3

You have to call your c function :

@c()

Without parens, you're only accessing it. Remember, CoffeeScript isn't Ruby ;).

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

1 Comment

A million thank yous. Because CoffeScript allows you to not but the brackets like this: @a evt, it was throwing me a bit to see one was working and not the other. With @c() it works fine now :)
0

I dont think you need the evt since you are not using it.

$('#c').on 'click', () => @c

3 Comments

Is this relevant? Surely an unused variable will make no difference whatsoever.
@Jivings, It is not pure JS so, may be it compiles funny.
I've removed the evt but the click handler does not work. I've updated my question with some more information.

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.