# Clearing out submitted form

**URL:** <https://www.ruby-forum.com/t/clearing-out-submitted-form/86998>\
**Category:** Rails\
**Created:** [February 27, 2007, 2:04pm UTC](https://www.ruby-forum.com/t/clearing-out-submitted-form/86998 "2007-02-27T14:04:27Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![kturner](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/c2a13f/32.png) [@kturner](https://www.ruby-forum.com/u/kturner)\
**Post date:** [February 27, 2007, 2:04pm UTC](https://www.ruby-forum.com/t/clearing-out-submitted-form/86998/1 "2007-02-27T14:04:27Z")

</div>

I’m trying to put together a basic blog application for fun, and I’m  
using an RJS template to insert a new comment into existing comments.  
The comment data is inserted, but the comment form at the bottom of the  
page still contains the comment. I tried to clear out the text from the  
form this way:

page.insert\_html :top, ‘comments’, :partial =\> ‘show\_comment’  
page.replace\_html ‘blog\_entry\_comment\_body’, ‘’  
page.replace\_html ‘blog\_entry\_comment\_subject’, ‘’

But this doesn’t work either. Any thoughts on how to clear out the form  
contents after the comment is added? The form looks like this:

\<% form\_remote\_tag(:url =\> {:action =\> ‘ajax\_add\_comment’, :controller  
=\> ‘Comment’, :id =\> @blog\_entry.id }) do %\>

```
<%= render :partial=>"comment/form" %>
<%= submit_tag 'Add Comment' %>

```

\<% end %\>

Firefox may also be putting the data in the form, as part of it’s  
auto-completion functionality, I suppose. Same behavior is present in  
IE7.

Thanks in advance,  
Kevin

---

<div class="post-metadata">

**Author:** ![kturner](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/c2a13f/32.png) [@kturner](https://www.ruby-forum.com/u/kturner)\
**Post date:** [February 27, 2007, 3:28pm UTC](https://www.ruby-forum.com/t/clearing-out-submitted-form/86998/2 "2007-02-27T15:28:59Z")

</div>

> page.replace\_html ‘blog\_entry\_comment\_body’, ‘’  
> page.replace\_html ‘blog\_entry\_comment\_subject’, ‘’

this isn’t doing what you think it is doing. it replaces the contents  
of tags, not the values. (actually, replace\_html will work on a  
textarea, but will not on type=“text” input)

you want to do something like:

# blog\_entry\_comment\_body =\> textarea

page \<\< “$(‘blog\_entry\_comment\_body’).value = ‘’”

# also works

#page.replace\_html(‘blog\_entry\_comment\_body’, ‘’)

# blog\_entry\_comment\_subject =\> input type=“text”

page \<\< “$(‘blog\_entry\_comment\_subject’).value = ‘’”

you are submitting the form via ajax, so therefore the page is not  
being ‘reloaded’ and thus firefox or IE is not auto-filing the forms  
for you at this point because you never left the page.

---

<div class="post-metadata">

**Author:** ![kturner](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/c2a13f/32.png) [@kturner](https://www.ruby-forum.com/u/kturner)\
**Post date:** [February 28, 2007, 3:58pm UTC](https://www.ruby-forum.com/t/clearing-out-submitted-form/86998/3 "2007-02-28T15:58:51Z")

</div>

You can also use Collection Proxies in RJS like so :

page.select(‘div textarea’).each do |element|  
element.value=""  
end

This takes all of the text areas in a div and replaces the value. You  
could also use the div name like so :

page.select(’#id\_of\_textarea\_here’).each do |element|  
element.value=""  
end

So if I had a \<textarea id=“id\_of\_textarea\_here” …

That should replace the value.

Hamza
