Colin Wu Colin Wu - 1 year ago 142
CoffeeScript Question

Coffeescript: How to retrieve element content after it has changed

This is probably a fairly rudimentary Coffeescript question: I want to retrieve the content of a textbox element AFTER is has changed. I have tried the following:

$("#notes").change (e) ->
alert("Note content changed. Is now " + $("#notes").text())

where "notes" is the ID of a textbox.

This does not work. It always displays the original content of the "notes" textbox. I suspect it is because the coffeescript is being compiled to javascript at load time.

Is what I want to do possible with Coffeescript? If so, please show me.


Answer Source

You read value from textarea in wrong way.

Without jQuery:

$("#notes").change (e) ->
  alert("Note content changed. Is now #{ e.currentTarget.value }")

With jQuery:

$("#notes").change (e) ->
  alert("Note content changed. Is now #{ $(e.currentTarget).val() }")

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download