RJS replace the value of a textfield

Is there a possibility to replace the value of a textfield within rjs?
I am calling a link_to_remote within a form, to get some data. Now I
want to fill in that data in the existing form. The problem is that I
can replace the html, but I can’t replace the
value=“text_i_want_to_change”, but that is the thing that you see and
that is submitted by the form.

Any suggestions?

In your .rjs:

page[“text_field_id”].value = new_value

Jason

Hi Pietropizzi,

Pietropizzi wrote:

Is there a possibility to replace the value of a textfield
within rjs? I am calling a link_to_remote within a form,
to get some data. Now I want to fill in that data in the
existing form. The problem is that I can replace the html,
but I can’t replace the value=“text_i_want_to_change”,
but that is the thing that you see and that is submitted by
the form.

Any suggestions?

Two possibilities. First, a little background. Javascript acts on DOM
elements that it typically finds by their id, less often by their name.
RJS
gives you two options: ‘replace’ which will replace the entire contents with a new one, and
‘replace_html’
which will replace the contents within the but leave the ‘wrapper’ alone. Each
is an
option you could use. Either way, you’re going to have to either put a
unique id on the textfield itself, or wrap it in another element that
has a
unique id. (e.g., <div id=‘unique_identifier>) A lot of Rails
methods call for element name (which is not required to be unique) and
then,
unless you specify otherwise, also use that name as the elements’ id.
That
can give you a rendered page with lots of elements having the same id.
Result: your RJS will not work. Use Firebug to inspect the DOM.

Easiest option to understand is to wrap the textfield, then use
page.replace_html with a partial that feeds the value you sent back to
the
controller with link_to_remote as a local.

If you haven’t already picked it up, I strongly recommend Cody F.'s
RJS
tutorial. Best $10 bucks you’ll spend learning Rails / Ajax. It’s a PDF
available at O’Reilly and will have you done with this and moving
forward in
very short time.

hth,
Bill