Colin Wu Colin Wu - 4 months ago 12
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.

Thanks.

Answer

You read value from textarea in wrong way.

Without jQuery:

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

https://jsfiddle.net/sr3tkaw7/3/

With jQuery:

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

https://jsfiddle.net/sr3tkaw7/4/

Comments