# Clearing a form

**URL:** <https://www.ruby-forum.com/t/clearing-a-form/109010>\
**Category:** Rails\
**Created:** [July 10, 2007, 5:01pm UTC](https://www.ruby-forum.com/t/clearing-a-form/109010 "2007-07-10T17:01:09Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![snantz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c2a13f/32.png) [@snantz](https://www.ruby-forum.com/u/snantz)\
**Post date:** [July 10, 2007, 5:01pm UTC](https://www.ruby-forum.com/t/clearing-a-form/109010/1 "2007-07-10T17:01:09Z")

</div>

Hi all,

I am trying to clear a form once the info is being submitted to the  
database. Basically what I have is a form that makes use of the  
form\_remote\_tag which adds a list of credit cards to a list and then  
adds that info to a database. What I want to happen is that once the  
submit button is hit the form clears so that the user can enter another  
credit card if they so choose. what I have tried is adding an :onclick  
to my submit\_tag helper:

\<%= submit\_tag "Add Credit Card Information, :onclick =\>  
‘clear\_credit\_card\_form()’ %\>

which called a JavaScript method that bascially went like:

document.getElementById( “exp\_date” ).value = " ";

to just clear the form, which it did. However, it cleared the form  
before the POST so nothing got through to the database and an exception  
was thrown. Anyone have any hints and how I could handle this. Thanks,

~S

---

<div class="post-metadata">

**Author:** ![snantz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c2a13f/32.png) [@snantz](https://www.ruby-forum.com/u/snantz)\
**Post date:** [July 10, 2007, 5:16pm UTC](https://www.ruby-forum.com/t/clearing-a-form/109010/2 "2007-07-10T17:16:27Z")

</div>

Shandy N. wrote:

> Hi all,
> 
> I am trying to clear a form once the info is being submitted to the  
> database. Basically what I have is a form that makes use of the  
> form\_remote\_tag which adds a list of credit cards to a list and then  
> adds that info to a database. What I want to happen is that once the  
> submit button is hit the form clears so that the user can enter another  
> credit card if they so choose. what I have tried is adding an :onclick  
> to my submit\_tag helper:
> 
> \<%= submit\_tag "Add Credit Card Information, :onclick =\>  
> ‘clear\_credit\_card\_form()’ %\>
> 
> which called a JavaScript method that bascially went like:
> 
> document.getElementById( “exp\_date” ).value = " ";
> 
> to just clear the form, which it did. However, it cleared the form  
> before the POST so nothing got through to the database and an exception  
> was thrown. Anyone have any hints and how I could handle this. Thanks,
> 
> ~S

Hello

Are you using rjs? If so, i think you can use  
page[‘form\_id’].reset  
in your create.rjs

---

<div class="post-metadata">

**Author:** ![snantz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c2a13f/32.png) [@snantz](https://www.ruby-forum.com/u/snantz)\
**Post date:** [July 10, 2007, 6:05pm UTC](https://www.ruby-forum.com/t/clearing-a-form/109010/3 "2007-07-10T18:05:28Z")

</div>

I’m a noobie at this, so this might seem like a dumb question, but how  
do you add an id to the form tag? What I have is:

\<%= form\_remote\_tag :update =\> “credit\_card\_table”, :url =\> { :action =\>  
:add\_credit\_card, :id =\> @user } %\>

if I do something like:

\<%= form\_remote\_tag :update =\> “credit\_card\_table”, :id =\>  
“credit\_card\_form”, :url =\> { :action =\> :add\_credit\_card, :id =\> @user  
} %\>

the raw html doesn’t show an id attirbute and thus my .rjs file does  
nothing. Another thing is what do I call the .rjs file? If the file that  
has the form is edit\_form.rhtml should I name my rjs file edit\_form.rjs?  
I’m just a little confused on how the code in the .rjs file get excuted.  
Thanks,

~S

---

<div class="post-metadata">

**Author:** ![snantz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c2a13f/32.png) [@snantz](https://www.ruby-forum.com/u/snantz)\
**Post date:** [July 10, 2007, 7:45pm UTC](https://www.ruby-forum.com/t/clearing-a-form/109010/4 "2007-07-10T19:45:24Z")

</div>

Im not sure what’s going on. I have a web apps book for Ruby on Rails  
which describes the same things that you have been suggesting as far as  
the .rjs file goes but it is not working. I have matched the method name  
with the .rjs file and have given all the html elements the proper id’s,  
but it’s still not working. I don’t think it is excuting the .rjs file,  
but I don’t see why. I’ll keep messing with. Thanks for your help. It’s  
all about learning new things and I have learned a lot even if it’s not  
cooperating.

~S

---

<div class="post-metadata">

**Author:** ![snantz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c2a13f/32.png) [@snantz](https://www.ruby-forum.com/u/snantz)\
**Post date:** [July 10, 2007, 6:28pm UTC](https://www.ruby-forum.com/t/clearing-a-form/109010/5 "2007-07-10T18:28:23Z")

</div>

Shandy N. wrote:

> I’m a noobie at this, so this might seem like a dumb question, but how  
> do you add an id to the form tag? What I have is:
> 
> \<%= form\_remote\_tag :update =\> “credit\_card\_table”, :url =\> { :action =\>  
> :add\_credit\_card, :id =\> @user } %\>
> 
> if I do something like:
> 
> \<%= form\_remote\_tag :update =\> “credit\_card\_table”, :id =\>  
> “credit\_card\_form”, :url =\> { :action =\> :add\_credit\_card, :id =\> @user  
> } %\>
> 
> the raw html doesn’t show an id attirbute and thus my .rjs file does  
> nothing. Another thing is what do I call the .rjs file? If the file that  
> has the form is edit\_form.rhtml should I name my rjs file edit\_form.rjs?  
> I’m just a little confused on how the code in the .rjs file get excuted.  
> Thanks,
> 
> ~S

1. 

Try

\<%= form\_remote\_tag  
:update =\> “credit\_card\_table”,  
:url =\> { :action =\> :add\_credit\_card, :id =\> @user},  
:html =\> { :id = ‘form\_id’ }%\>

By the way check out form\_remote\_for, i think this helper fits better  
your needs.([http://api.rubyonrails.org/](http://api.rubyonrails.org/))

1. 

You should name your rjs file after the method you are trying to execute  
in your controller. So create method will have create.rjs.

---

<div class="post-metadata">

**Author:** ![snantz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c2a13f/32.png) [@snantz](https://www.ruby-forum.com/u/snantz)\
**Post date:** [July 10, 2007, 10:21pm UTC](https://www.ruby-forum.com/t/clearing-a-form/109010/6 "2007-07-10T22:21:21Z")

</div>

SHAZAM - I figured it out - well not really, I just figured out a new  
way to do it. So here it is; in my form\_remote\_tag I added:

:complete=\>'document.getElementById(“exp\_date”).value = “”;

The problem taht I had before was that I was using this exact same  
JavaScript to clear the form, but it cleared it before it posted the  
info, making is usless, plus it crashed on top of that. I imagine what  
:complete does is post first then execute the JavaScript. But that’s how  
I got it to work, now I’m going on break!

~Shandy
