Forum: Ruby on Rails RJS replace the value of a textfield

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Pietropizzi (Guest)
on 2007-02-27 20:34
(Received via mailing list)
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?
Jason R. (Guest)
on 2007-02-27 21:14
(Received via mailing list)
In your .rjs:

page["text_field_id"].value = new_value

Jason
Bill W. (Guest)
on 2007-02-27 21:39
(Received via mailing list)
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 <element
id='unique_identifier'>contents</element> with a new one, and
'replace_html'
which will replace the contents within the <element
id='unique_identifier'></element> 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></div>)  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
This topic is locked and can not be replied to.