0

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?

1 Answer 1

4

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()
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks vee, that worked. In this case I have a text input box so I used a blur event to get the input.

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.