# Accessing form elements

**URL:** <https://discuss.rubyonrails.org/t/accessing-form-elements/30189>\
**Category:** rubyonrails-talk\
**Created:** [June 3, 2008, 10:46pm UTC](https://discuss.rubyonrails.org/t/accessing-form-elements/30189 "2008-06-03T22:46:35Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![11175](https://avatars.discourse-cdn.com/v4/letter/1/8edcca/32.png) [@11175](https://discuss.rubyonrails.org/u/11175)\
**Post date:** [June 3, 2008, 10:46pm UTC](https://discuss.rubyonrails.org/t/accessing-form-elements/30189/1 "2008-06-03T22:46:35Z")

</div>

I posted a question earlier but it didn't come out that clear and I have spent the whole day looking this up. Any help would be much appreciated.

Here goes round 2:

I have several textareas on the screen (anywhere from 1 - 5). Right now I have them in a form and am using a submit button to pass the values to the controller. What I need to do is pass these values to the controller using links located outside of the actual form. When the link is click the values from the textareas are passed to the controller along with a :question parameter. The values of the textareas are saved into the db and the :question param is used in a query and then the page is redirected to another page with the result from the query as another param.

The part I cannot seem to find an answer for is how to pass the values of the textareas to the controller using link\_to(), not link\_to\_remote. Or how do you access the textarea values and attach them to :value1 =\> ???

I really hope that made more sense.

Thanks,

Chris

---

<div class="post-metadata">

**Author:** ![Frederick\_Cheung](https://sea2.discourse-cdn.com/rubyonrails/user_avatar/discuss.rubyonrails.org/frederick_cheung/32/3901_2.png) [@Frederick\_Cheung](https://discuss.rubyonrails.org/u/Frederick_Cheung)\
**Post date:** [June 3, 2008, 10:55pm UTC](https://discuss.rubyonrails.org/t/accessing-form-elements/30189/2 "2008-06-03T22:55:58Z")

</div>

It's got to either be javascript or a form submit(and you need javascript to trigger that from a regular link).

Fred

---

<div class="post-metadata">

**Author:** ![11175](https://avatars.discourse-cdn.com/v4/letter/1/8edcca/32.png) [@11175](https://discuss.rubyonrails.org/u/11175)\
**Post date:** [June 3, 2008, 11:02pm UTC](https://discuss.rubyonrails.org/t/accessing-form-elements/30189/3 "2008-06-03T23:02:51Z")

</div>

I initially tried it with a remote\_function and used that with an onunload event but it would only work 50% of the time. I also tried onclick with the same remote\_function but I think the responses were too long and it would only grab half of it before moving on.

Oh well, I guess I am going to have to say I can't do it the way they want it.

Thanks anyways,

chris

---

<div class="post-metadata">

**Author:** ![11175](https://avatars.discourse-cdn.com/v4/letter/1/8edcca/32.png) [@11175](https://discuss.rubyonrails.org/u/11175)\
**Post date:** [June 3, 2008, 11:04pm UTC](https://discuss.rubyonrails.org/t/accessing-form-elements/30189/4 "2008-06-03T23:04:14Z")

</div>

Sorry I misread your response. How would I trigger the form submit from a link? That is what I have been looking to do all day and could only find ajaxy ways of doing it which wouldnt allow me to redirect afterwards.

---

<div class="post-metadata">

**Author:** ![Frederick\_Cheung](https://sea2.discourse-cdn.com/rubyonrails/user_avatar/discuss.rubyonrails.org/frederick_cheung/32/3901_2.png) [@Frederick\_Cheung](https://discuss.rubyonrails.org/u/Frederick_Cheung)\
**Post date:** [June 3, 2008, 11:12pm UTC](https://discuss.rubyonrails.org/t/accessing-form-elements/30189/5 "2008-06-03T23:12:01Z")

</div>

onclick="some\_form.submit()". At that point you might as well just be styling the form's submit button to not look like a button.

Fred

---

<div class="post-metadata">

**Author:** ![Pardee\_Roy](https://avatars.discourse-cdn.com/v4/letter/p/f0a364/32.png) [@Pardee\_Roy](https://discuss.rubyonrails.org/u/Pardee_Roy)\
**Post date:** [June 5, 2008, 12:46am UTC](https://discuss.rubyonrails.org/t/accessing-form-elements/30189/6 "2008-06-05T00:46:15Z")

</div>

So you've got a traditional form generated by form\_for, with the expected submit button etc. That all works just as you want it to. Now you want to \*in addition to the existing submit button\* offer a link up at the top of the page that will submit that same form. Do you want that link to a) have exactly the same effect as the submit button--no more, no less, or b) have the same effect as the submit button \*plus\* tack an extra parameter & value onto :params? (Or c) something else?)

If a)--I would think Fred's earlier suggestion to use javascript to do a my\_form.submit() should work. I don't know the best way to get rails to generate that (link\_to\_function("click me", "my\_form.submit()") maybe?) but no doubt others here will. If b) it's probably a little more involved (javascript stuffs your parameter in a hidden field created in your form maybe, and \*then\* does a my\_form.submit()?).

HTH,

-Roy

---

<div class="post-metadata">

**Author:** ![11175](https://avatars.discourse-cdn.com/v4/letter/1/8edcca/32.png) [@11175](https://discuss.rubyonrails.org/u/11175)\
**Post date:** [June 5, 2008, 4:38pm UTC](https://discuss.rubyonrails.org/t/accessing-form-elements/30189/7 "2008-06-05T16:38:10Z")

</div>

Roy Pardee wrote: or b) have the same effect as the submit button \*plus\*

> tack an extra parameter & value onto :params? (Or c) something else?)

If b) it's probably a little more

> involved (javascript stuffs your parameter in a hidden field created in your form maybe, and \*then\* does a my\_form.submit()?).

Exactly, I need to pass another parameter and that is where I am stuck right now. The only option I can think of is to use some ajax and have it pass a parm in when someone clicks the link and have it return nothing and then have that same click call form.submit() afterwards. I am about to try this now but it seems like 1) it will be really slow and 2) it might not work. We will see.

I just dont know a way to pass the extra param as well as submitting the form.

Thanks a lot,

Chris

---

<div class="post-metadata">

**Author:** ![11175](https://avatars.discourse-cdn.com/v4/letter/1/8edcca/32.png) [@11175](https://discuss.rubyonrails.org/u/11175)\
**Post date:** [June 5, 2008, 4:42pm UTC](https://discuss.rubyonrails.org/t/accessing-form-elements/30189/8 "2008-06-05T16:42:37Z")

</div>

You could simply use a hidden\_field

---

<div class="post-metadata">

**Author:** ![11175](https://avatars.discourse-cdn.com/v4/letter/1/8edcca/32.png) [@11175](https://discuss.rubyonrails.org/u/11175)\
**Post date:** [June 5, 2008, 4:48pm UTC](https://discuss.rubyonrails.org/t/accessing-form-elements/30189/9 "2008-06-05T16:48:44Z")

</div>

Thorsten Mueller wrote:

> You could simply use a hidden\_field

I could be wrong seeing I am not too familiar with hidden fields but I dont think that would help. I have a list of numbers at the top representing different questions. If the user clicks on "1" I want to pass in the "1" and submit the form, if they click "6" I want to pass in the "6" and submit the form. From what I understand about hidden fields there is no way to pass the "1" or "6" into the hidden field, correct?

Chris

---

<div class="post-metadata">

**Author:** ![11175](https://avatars.discourse-cdn.com/v4/letter/1/8edcca/32.png) [@11175](https://discuss.rubyonrails.org/u/11175)\
**Post date:** [June 5, 2008, 5:06pm UTC](https://discuss.rubyonrails.org/t/accessing-form-elements/30189/10 "2008-06-05T17:06:20Z")

</div>

Chris Hickman wrote:

> Thorsten Mueller wrote:
> 
> > You could simply use a hidden\_field
> 
> I could be wrong seeing I am not too familiar with hidden fields but I dont think that would help. I have a list of numbers at the top representing different questions. If the user clicks on "1" I want to pass in the "1" and submit the form, if they click "6" I want to pass in the "6" and submit the form. From what I understand about hidden fields there is no way to pass the "1" or "6" into the hidden field, correct?
> 
> Chris

nope, that's a perfect use case for a hidden field. those numbers should just do some js to set the value of the field

---

<div class="post-metadata">

**Author:** ![11175](https://avatars.discourse-cdn.com/v4/letter/1/8edcca/32.png) [@11175](https://discuss.rubyonrails.org/u/11175)\
**Post date:** [June 5, 2008, 5:12pm UTC](https://discuss.rubyonrails.org/t/accessing-form-elements/30189/11 "2008-06-05T17:12:05Z")

</div>

Thorsten Mueller wrote:

> nope, that's a perfect use case for a hidden field. those numbers should just do some js to set the value of the field

Oh so it keeps everything client side so it wont be as slow and then the value will get passed in with the form. So, I can just put a call to a javascript method inside the hidden field for :value =\> and it will get passed through correctly.

---

<div class="post-metadata">

**Author:** ![11175](https://avatars.discourse-cdn.com/v4/letter/1/8edcca/32.png) [@11175](https://discuss.rubyonrails.org/u/11175)\
**Post date:** [June 5, 2008, 5:17pm UTC](https://discuss.rubyonrails.org/t/accessing-form-elements/30189/12 "2008-06-05T17:17:59Z")

</div>

> Oh so it keeps everything client side so it wont be as slow and then the value will get passed in with the form. So, I can just put a call to a javascript method inside the hidden field for :value =\> and it will get passed through correctly.

ok, it depends on how exactly you use the hidden field (and if it's hidden\_field or hidden\_field\_tag)

most simple case:

\<%= hidden\_field\_tag 'number' %\>

will generate:

\<input id="number" name="number" type="hidden" /\>

so the number can define an onchange = "$('number').value = '1'"

the id/name may depend on your setup, but just have a look at the generated html to get that.

---

<div class="post-metadata">

**Author:** ![11175](https://avatars.discourse-cdn.com/v4/letter/1/8edcca/32.png) [@11175](https://discuss.rubyonrails.org/u/11175)\
**Post date:** [June 5, 2008, 5:22pm UTC](https://discuss.rubyonrails.org/t/accessing-form-elements/30189/13 "2008-06-05T17:22:00Z")

</div>

That makes sense. Thanks a lot, I really appreciate your help.

Chris
