I have an AJAX based application that updates a text field in a form depending on user input. The text field is defined as follows:
<%= form.text_field :queue, {:class => "queue_class", :id => "queue", :size => 40} %>
the Javascript code is the following:
document.getElementById('queue').value = <%= @returned_value %>
If in the controller, I set @returned_value to a number, like:
@returned_value='77777777777'
then is works fine. But if I set it to a string value like:
@returned_value='abcxyz'
then it does not work, the text field is not updated and I don’t get any error message, it’s like it does nothing.
I also tried this:
document.getElementById('queue').innerHTML = <%= @returned_value %>
but it does not work with any returned value, neither when @returned_value='77777777777' nor when @returned_value='abcxyz'
Can someone please point me to what am I missing here?
You’re missing quotes around the ERb expression:
(And it should be JavaScript-escaped as well.)
A number is a legal JavaScript expression without quotes. Arbitrary strings aren’t; JS will try to parse it as an expression (reference) plus whatever else is in the string that’s relevant to JS.