Reputation: 5325
I'm trying to get user input from a rails form, and assign that input to an object in coffeescript. My view looks like...
<div class="from_date">
From Date
<input type="text" id="from_date" name="from_date"></input>
</div>
...and my coffee script is...
jQuery ->
input = @value
alert @value
I am getting "undefined" in my alert box. What am I missing?
Upvotes: 0
Views: 5927
Reputation: 38645
You cannot do that because your view is server side and coffee script is client side. Ruby variables cannot be accessed in JavaScript unless the JavaScript code is embedded within the server side script. However, embedding javascript within server side view should be avoided and kept separate.
You could get the value of the from_date
input field using cofeescript as follows:
jQuery ->
input = $('#from_date').val()
This will assign the value in from_date
input field to input
variable. Note that this assignment happens as soon as the DOM is ready.
I'm unsure if your requirement is to capture the value of from_date
on DOM ready. Usually, these assignments are done on some event, say a button click. For such case you'd do:
jQuery ->
$('#my_button').click (evt) ->
input = $('#from_date').val()
*edit by Lumbee What I used was...
jQuery ->
$("#from_date").blur ->
alert $('#from_date').val()
Upvotes: 4