# Re-render a partial when a form field is updated

**URL:** <https://www.ruby-forum.com/t/re-render-a-partial-when-a-form-field-is-updated/179782>\
**Category:** Rails\
**Created:** [December 3, 2009, 6:27pm UTC](https://www.ruby-forum.com/t/re-render-a-partial-when-a-form-field-is-updated/179782 "2009-12-03T18:27:26Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![scrozier](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/48db29/32.png) [@scrozier](https://www.ruby-forum.com/u/scrozier)\
**Post date:** [December 3, 2009, 6:27pm UTC](https://www.ruby-forum.com/t/re-render-a-partial-when-a-form-field-is-updated/179782/1 "2009-12-03T18:27:26Z")

</div>

I would like to learn how to update a partial when a text field is  
updated. What would be the best way to go about doing this? I’m sort  
of stuck on how to generally accomplish this.

I’m basically creating a page with a list of contacts, and at the top,  
there is a search box that will filter the contacts based on what the  
user submits. With javascript disabled, it will re-display the page  
with the filtered contacts. With it enabled, it will dynamically update  
the div with the list of contacts as the user types in the text field.

Thanks for any help in advance.

---

<div class="post-metadata">

**Author:** ![scrozier](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/48db29/32.png) [@scrozier](https://www.ruby-forum.com/u/scrozier)\
**Post date:** [December 3, 2009, 6:29pm UTC](https://www.ruby-forum.com/t/re-render-a-partial-when-a-form-field-is-updated/179782/2 "2009-12-03T18:29:11Z")

</div>

Steve C. wrote:

> I would like to learn how to update a partial when a text field is  
> updated. What would be the best way to go about doing this? I’m sort  
> of stuck on how to generally accomplish this.

Have you looked at observe\_field or a simple JS onchange?

> I’m basically creating a page with a list of contacts, and at the top,  
> there is a search box that will filter the contacts based on what the  
> user submits. With javascript disabled, it will re-display the page  
> with the filtered contacts.

With JS disabled, you’ll need an actual submit button.

> With it enabled, it will dynamically update  
> the div with the list of contacts as the user types in the text field.
> 
> Thanks for any help in advance.

Best,  
–Â  
MarnenÂ Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![scrozier](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/48db29/32.png) [@scrozier](https://www.ruby-forum.com/u/scrozier)\
**Post date:** [December 3, 2009, 6:32pm UTC](https://www.ruby-forum.com/t/re-render-a-partial-when-a-form-field-is-updated/179782/3 "2009-12-03T18:32:50Z")

</div>

Marnen Laibow-Koser wrote:

> Have you looked at observe\_field or a simple JS onchange?  
> I’ve been looking over the API, but I guess I’m so new to Rails that I’m  
> having a hard time grasping out to get it to work correctly. I’ll  
> continue to investigate observe\_field; thanks for the tip.

> With JS disabled, you’ll need an actual submit button.  
> That’s what I was thinking - if the user has javascript disabled, then  
> I’d show a submit button; otherwise it’d be hidden. I think the first  
> step is to get it to work without javascript, so a simple search form to  
> update the page would be a good first step.

I’m watching this now to see if it helps:  
[http://media.railscasts.com/videos/037\_simple\_search\_form.mov](http://media.railscasts.com/videos/037_simple_search_form.mov)

---

<div class="post-metadata">

**Author:** ![scrozier](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/48db29/32.png) [@scrozier](https://www.ruby-forum.com/u/scrozier)\
**Post date:** [December 3, 2009, 6:38pm UTC](https://www.ruby-forum.com/t/re-render-a-partial-when-a-form-field-is-updated/179782/4 "2009-12-03T18:38:29Z")

</div>

Steve C. wrote:

> Marnen Laibow-Koser wrote:
> 
> > Have you looked at observe\_field or a simple JS onchange?  
> > I’ve been looking over the API, but I guess I’m so new to Rails that I’m  
> > having a hard time grasping out to get it to work correctly. I’ll  
> > continue to investigate observe\_field; thanks for the tip.
> 
> > With JS disabled, you’ll need an actual submit button.  
> > That’s what I was thinking - if the user has javascript disabled, then  
> > I’d show a submit button; otherwise it’d be hidden. I think the first  
> > step is to get it to work without javascript, so a simple search form to  
> > update the page would be a good first step.

Prototype has a function to Ajaxify a form and hide the submit button.  
I think there’s a Rails helper for it too, but I avoid the Rails helpers  
– they mix JS into HTML, which is usually a bad idea.

> I’m watching this now to see if it helps:  
> [http://media.railscasts.com/videos/037\_simple\_search\_form.mov](http://media.railscasts.com/videos/037_simple_search_form.mov)

Best,  
–Â  
MarnenÂ Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![scrozier](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/48db29/32.png) [@scrozier](https://www.ruby-forum.com/u/scrozier)\
**Post date:** [December 3, 2009, 9:19pm UTC](https://www.ruby-forum.com/t/re-render-a-partial-when-a-form-field-is-updated/179782/5 "2009-12-03T21:19:31Z")

</div>

Marnen Laibow-Koser wrote:

> Have you looked at observe\_field or a simple JS onchange?

So I’ve been experimenting with the observe\_field and observe\_form, but  
I can’t seem to get it to respond to any updates. My first logical next  
step was to make sure the DOM id was being specified correctly, but I  
double checked that and tried it for both a text field and the form  
itself (using observe\_form) and nothing happens.

I opened up Firebug in FF to see if there was anything going on in the  
console, but alas - it’s void of any updates when I enter text into the  
search form.

Here’s the code I’m using at the moment. Can anyone spot any errors?

\<% observe\_field ‘search\_contacts\_form’,  
:function =\> “alert(‘Element changed’)”  
%\>

For the moment, I’m just trying to make the browser give an alert to  
make sure it’s monitoring the changes.
