I’m trying to do some simple JavaScript in Rails but am having a lot of
difficulty getting it to work.
Basically, I have a form with a text field. When the user types in the
text field, I want to respond to each key press event and update another
field on the screen.
I want to do this without a round-trip to the server.
In straight HTML, it would look something like this:
I’ve tried many permutations of the text_field_tag and none of them seem
to work as I expect. The closest I got was
Have you tried using another function name besides “test.” Initially
this confused me too, but then I thought maybe “test” is a keyword in
JavaScript so I tried something else and it seems to work just fine.
Craig User wrote:
i.e. this doesn’t work
<%= text_field_tag ‘answer’, nil, :onkeypress => “test(this)” %>
function getStyle(answer) {
var styleField = document.getElementById(‘style’);
styleField.innerHTML = answer
}
In the interests of DRY though, does anyone know how to pass in a
non-form variable to my javascript function so that I don’t have to do
the document.getElementById lookup?
i.e.
getStyle($F(‘answer’), $P(‘otherElement’))
I guess I’m asking if there’s a ‘page’ equivalent to the ‘form’
shorthand “$F”.
On Tue, Aug 19, 2008 at 6:29 PM, Craig User [email protected] wrote:
In the interests of DRY though, does anyone know how to pass in a
non-form variable to my javascript function so that I don’t have to do
the document.getElementById lookup?