# RJS replace the value of a textfield

**URL:** <https://www.ruby-forum.com/t/rjs-replace-the-value-of-a-textfield/87039>\
**Category:** Rails\
**Created:** [February 27, 2007, 7:34pm UTC](https://www.ruby-forum.com/t/rjs-replace-the-value-of-a-textfield/87039 "2007-02-27T19:34:06Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Pietropizzi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/35a633/32.png) [@Pietropizzi](https://www.ruby-forum.com/u/Pietropizzi)\
**Post date:** [February 27, 2007, 7:34pm UTC](https://www.ruby-forum.com/t/rjs-replace-the-value-of-a-textfield/87039/1 "2007-02-27T19:34:06Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Pietropizzi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/35a633/32.png) [@Pietropizzi](https://www.ruby-forum.com/u/Pietropizzi)\
**Post date:** [February 27, 2007, 8:14pm UTC](https://www.ruby-forum.com/t/rjs-replace-the-value-of-a-textfield/87039/2 "2007-02-27T20:14:18Z")

</div>

In your .rjs:

page[“text\_field\_id”].value = new\_value

Jason

---

<div class="post-metadata">

**Author:** ![Pietropizzi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/35a633/32.png) [@Pietropizzi](https://www.ruby-forum.com/u/Pietropizzi)\
**Post date:** [February 27, 2007, 8:39pm UTC](https://www.ruby-forum.com/t/rjs-replace-the-value-of-a-textfield/87039/3 "2007-02-27T20:39:22Z")

</div>

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
