# Fire submit when drop down box item clicked

**URL:** <https://www.ruby-forum.com/t/fire-submit-when-drop-down-box-item-clicked/160419>\
**Category:** Rails\
**Created:** [February 16, 2009, 6:45am UTC](https://www.ruby-forum.com/t/fire-submit-when-drop-down-box-item-clicked/160419 "2009-02-16T06:45:12Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![hangingclowns](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/b5e925/32.png) [@hangingclowns](https://www.ruby-forum.com/u/hangingclowns)\
**Post date:** [February 16, 2009, 6:45am UTC](https://www.ruby-forum.com/t/fire-submit-when-drop-down-box-item-clicked/160419/1 "2009-02-16T06:45:12Z")

</div>

This may not be rails specific, but i have a select drop down box and  
want the “submit” action fired when the user clicks one of the options  
in my drop down select tag box. How do I accomplish this?

thanks

---

<div class="post-metadata">

**Author:** ![hangingclowns](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/b5e925/32.png) [@hangingclowns](https://www.ruby-forum.com/u/hangingclowns)\
**Post date:** [February 16, 2009, 7:09am UTC](https://www.ruby-forum.com/t/fire-submit-when-drop-down-box-item-clicked/160419/2 "2009-02-16T07:09:25Z")

</div>

Allen W. wrote:

> This may not be rails specific, but i have a select drop down box and  
> want the “submit” action fired when the user clicks one of the options  
> in my drop down select tag box. How do I accomplish this?
> 
> thanks

Hey,

You need to call Ajax request onChange event of dropdown.

you can follow this steps.

1. add onChange event with select tag.
2. call your responsible action using Ajax request.
3. render update your rhtml.

---

<div class="post-metadata">

**Author:** ![hangingclowns](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/b5e925/32.png) [@hangingclowns](https://www.ruby-forum.com/u/hangingclowns)\
**Post date:** [February 16, 2009, 3:05pm UTC](https://www.ruby-forum.com/t/fire-submit-when-drop-down-box-item-clicked/160419/3 "2009-02-16T15:05:01Z")

</div>

Hitesh R. wrote:

> Allen W. wrote:
> 
> > This may not be rails specific, but i have a select drop down box and  
> > want the “submit” action fired when the user clicks one of the options  
> > in my drop down select tag box. How do I accomplish this?
> > 
> > thanks
> 
> Hey,
> 
> You need to call Ajax request onChange event of dropdown.
> 
> you can follow this steps.
> 
> 1. add onChange event with select tag.
> 2. call your responsible action using Ajax request.
> 3. render update your rhtml.

Actually AJAX isn’t necessary for this. All the OP asked to do is submit  
the form. He can do that with by adding something like this to his  
onchange event of his drop-down list:

onchange=“myForm.submit();”

Or even better would be to add an observer to the drop-down or to the  
form. This is becoming the preferred method because it uses an  
“unobtrusive JavaScript” approach.

See the Prototype Element.observe for details on how to use observers:

> **[Prototype API Documentation | Element.observe (Deprecated URL)](http://prototypejs.org/api/element/observe)**

Or jQuery Events if you prefer that:

> **[Events | jQuery API Documentation](https://api.jquery.com/category/events/)**

Just remember with this approach you need to wait for the DOM to load  
before adding the observer. See the jQuery ready() method for more  
information about that. Prototype also has a way to do this, but isn’t  
as elegant as jQuery’s ready() method.

---

<div class="post-metadata">

**Author:** ![hangingclowns](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/b5e925/32.png) [@hangingclowns](https://www.ruby-forum.com/u/hangingclowns)\
**Post date:** [February 16, 2009, 10:14pm UTC](https://www.ruby-forum.com/t/fire-submit-when-drop-down-box-item-clicked/160419/4 "2009-02-16T22:14:10Z")

</div>

So let’s say I have:

\<% form\_for :skus do |f|%\>  
Product: \<%= f.text\_field :name, :autocomplete =\> “off”, %\>  
\<%= submit\_tag “Get Product” %\>  
\<% end -%\>

then how should I do the prototype observe code? I tried and no alert  
fired:

Robert W. wrote:

> Hitesh R. wrote:
> 
> > Allen W. wrote:
> > 
> > > This may not be rails specific, but i have a select drop down box and  
> > > want the “submit” action fired when the user clicks one of the options  
> > > in my drop down select tag box. How do I accomplish this?
> > > 
> > > thanks
> > 
> > Hey,
> > 
> > You need to call Ajax request onChange event of dropdown.
> > 
> > you can follow this steps.
> > 
> > 1. add onChange event with select tag.
> > 2. call your responsible action using Ajax request.
> > 3. render update your rhtml.
> 
> Actually AJAX isn’t necessary for this. All the OP asked to do is submit  
> the form. He can do that with by adding something like this to his  
> onchange event of his drop-down list:
> 
> onchange=“myForm.submit();”
> 
> Or even better would be to add an observer to the drop-down or to the  
> form. This is becoming the preferred method because it uses an  
> “unobtrusive JavaScript” approach.
> 
> See the Prototype Element.observe for details on how to use observers:  
> [Prototype API Documentation | Element.observe (Deprecated URL)](http://www.prototypejs.org/api/element/observe)
> 
> Or jQuery Events if you prefer that:  
> [Events | jQuery API Documentation](http://docs.jquery.com/Events)
> 
> Just remember with this approach you need to wait for the DOM to load  
> before adding the observer. See the jQuery ready() method for more  
> information about that. Prototype also has a way to do this, but isn’t  
> as elegant as jQuery’s ready() method.
