Forum: Ruby on Rails select another value in a combo box with a button click

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Cdcdda30ac4e349c6ffd0819b9e1666f?d=identicon&s=25 Jay Pangmi (jaeezzy)
on 2008-10-10 02:47
Hi, I was wondering if anyone can help me with this as I just don't seem
to figure out how I can refer to a combo box and change its selected
value to another with a button click.
Suppose a cambo box has values '1', '2', and '3' and I have a button
"Next" and that currently '1' is selected in the combo box. So, when I
click "Next" button I want it to select '2' and so on...

Thanks.
2b891e820c238ded365d035771603f21?d=identicon&s=25 Bill Walton (Guest)
on 2008-10-10 03:29
(Received via mailing list)
Hi Jay,

Jay Pangmi wrote:
>
> Hi, I was wondering if anyone can help me with this as I just don't seem
> to figure out how I can refer to a combo box and change its selected
> value to another with a button click.
> Suppose a cambo box has values '1', '2', and '3' and I have a button
> "Next" and that currently '1' is selected in the combo box. So, when I
> click "Next" button I want it to select '2' and so on...

If I understand your question,  one way I've done it is to put the combo
box
in a partial with the selected value 'fed' and then use Ajax / RJS to
re-render the partial as part of the "Next" action.

HTH,
Bill
23e4fc3add5443f51e55663cfc245d24?d=identicon&s=25 Erol Fornoles (Guest)
on 2008-10-10 04:55
(Received via mailing list)
Jay Pangmi wrote:
> Hi, I was wondering if anyone can help me with this as I just don't seem
> to figure out how I can refer to a combo box and change its selected
> value to another with a button click.
> Suppose a cambo box has values '1', '2', and '3' and I have a button
> "Next" and that currently '1' is selected in the combo box. So, when I
> click "Next" button I want it to select '2' and so on...
>
> Thanks.
> --
> Posted via http://www.ruby-forum.com/.

We can achieve the same effect purely on the client-side.

Assuming your select dropdown is named "dropdown":

<input type="button" value="Next" onclick="if($('dropdown').length - 1
> $('dropdown').selectedIndex) $('dropdown').selectedIndex++;" />

This requires Prototype, btw.
Cdcdda30ac4e349c6ffd0819b9e1666f?d=identicon&s=25 Jay Pangmi (jaeezzy)
on 2008-10-10 06:16
Hi, guys thanks for all the help. Here's how I have created the combo
box:

<%=select :camp, :id, @campsites_list%>

So, how would I reference it and its a partial and its under
_campsites.rhtml (partial file).
Thanks.
23e4fc3add5443f51e55663cfc245d24?d=identicon&s=25 Erol Fornoles (Guest)
on 2008-10-10 06:54
(Received via mailing list)
On Oct 10, 12:16 pm, Jay Pangmi <rails-mailing-l...@andreas-s.net>
wrote:
> Posted viahttp://www.ruby-forum.com/.
I still think that a client-side JS is better here, since IMHO an AJAX
call just to select the next item of an already populated select
dropdown is simply too much.

<%= submit_tag "Next",
:onclick => "if($('camp').length - 1 > $('camp').selectedIndex) $
('camp').selectedIndex++; return false;" %>
Cdcdda30ac4e349c6ffd0819b9e1666f?d=identicon&s=25 Jay Pangmi (jaeezzy)
on 2008-10-10 07:54
Erol Fornoles wrote:
> <%= submit_tag "Next",
> :onclick => "if($('camp').length - 1 > $('camp').selectedIndex) $
> ('camp').selectedIndex++; return false;" %>

Thanks Erol Fornoles for the answer.. but I just couldn't make any use
of it coz it doen't work and I'm using <%=javascript_include_tag
"prototype.js"%>. To make clear what I'm doing here is my code:

VIEW:
================================================================================
<p>
    <label for = "walk location">Locations</label><br />
    <%= select(:walks, :id, Greatwalk.find_available_locations,
        {:prompt=>'Select Great Walk Location'}, :selected => nil, :id
=> :great_walks) %>
</p>
    <%= observe_field("great_walks", :url => {:controller => "user",
            :action => "update_campsites"},
                    :update => "campsites_list", :with => "id")%>
<%form_for :cart, :url => {:action => :add_to_cart} do |form|%>
  <div id ="campsites_list" >
      <p>
           <label for = "camp location">Campgrounds</label><br />
           <%= form.select "campsite", %w{ ---- }, :class =>
"select_campsites" %>
      </p>
  </div>
  <p>
    <label for = "arrival date">Arrival Date:</label><br />
    <%= form.text_field :arrival_date %>
  </p>
  <p>
    <label for = "nights">Nights:</label><br />
    <%= form.text_field :nights %>
  </p>
  <p>
    <label for = "parents">Parents:</label><br />
    <%= form.text_field :parents %>
  </p>
  <p>
    <label for = "children">Children:</label><br />
    <%= form.text_field :children %>
  </p>
  <p>
    <label for = "family claim">Family Rate:</label><br />
    <%= form.check_box "family_claim",{}, "1", "0"%>
  </p>

  <%=submit_tag "Calculate"%>
<%end%>
<%= submit_tag "Next",
:onclick => "if($('camp').length - 1 > $('camp').selectedIndex) $
('camp').selectedIndex++; return false;" %>
================================================================================

PARTIAL:
================================================================================
<p>
  <label for="camp location">Campgrounds</label><br/>
  <%=select "camp", "id", @resultset %>
</p>
================================================================================

As you can see, I will be updating combo box in the partial. Also, what
I want to do is make two buttons inside the form and if I click one it
will change the value of combo box to next and if I click another some
action will be call (add_to_cart in my case).
Thanks again..
23e4fc3add5443f51e55663cfc245d24?d=identicon&s=25 Erol Fornoles (Guest)
on 2008-10-10 08:40
(Received via mailing list)
On Oct 10, 1:54 pm, Jay Pangmi <rails-mailing-l...@andreas-s.net>
wrote:
> <p>
>   <label for="camp location">Campgrounds</label><br/>
>   <%=select "camp", "id", @resultset %>
> </p>

Where is the partial rendered? And when should it be rendered? I don't
see it in your main view.
Cdcdda30ac4e349c6ffd0819b9e1666f?d=identicon&s=25 Jay Pangmi (jaeezzy)
on 2008-10-10 09:16
Erol Fornoles wrote:
> On Oct 10, 1:54�pm, Jay Pangmi <rails-mailing-l...@andreas-s.net>
> wrote:
>> <p>
>> � <label for="camp location">Campgrounds</label><br/>
>> � <%=select "camp", "id", @resultset %>
>> </p>
>
> Where is the partial rendered? And when should it be rendered? I don't
> see it in your main view.

The file below is the partial which gets rendered in the <div> whose id
is "campsites_list" in the view inside the <%form_for%> ... <%end%>.
observe_field is observing the combo box with id :great_walks which is
the first combo box in the view and updates the combo box inside the
<%form_for%>..<%end%> inside <div> and this <div> is getting updated
with the following partial file:

_campsites.rhtml
--------------------------------------------------------------------------------
<p>
  <label for="camp location">Campgrounds</label><br/>
  <%=select "camp", "id", @resultset %>
</p>
--------------------------------------------------------------------------------

thanks...
23e4fc3add5443f51e55663cfc245d24?d=identicon&s=25 Erol Fornoles (Guest)
on 2008-10-10 10:10
(Received via mailing list)
On Oct 10, 1:54 pm, Jay Pangmi <rails-mailing-l...@andreas-s.net>
wrote:
> Thanks Erol Fornoles for the answer.. but I just couldn't make any use
> of it coz it doen't work and I'm using <%=javascript_include_tag
> "prototype.js"%>. To make clear what I'm doing here is my code:

Try this instead:

<%= submit_tag "Next", :onclick => "if($('camp_id').length - 1 > $
('camp_id').selectedIndex) $('camp_id').selectedIndex++; return
false;" %>
Cdcdda30ac4e349c6ffd0819b9e1666f?d=identicon&s=25 Jay Pangmi (jaeezzy)
on 2008-10-10 12:04
Erol Fornoles wrote:
> On Oct 10, 1:54�pm, Jay Pangmi <rails-mailing-l...@andreas-s.net>
> wrote:
>> Thanks Erol Fornoles for the answer.. but I just couldn't make any use
>> of it coz it doen't work and I'm using <%=javascript_include_tag
>> "prototype.js"%>. To make clear what I'm doing here is my code:
>
> Try this instead:
>
> <%= submit_tag "Next", :onclick => "if($('camp_id').length - 1 > $
> ('camp_id').selectedIndex) $('camp_id').selectedIndex++; return
> false;" %>

Thanks a tonn man... it works sweet. So, :camp, :id in the partial
becomes 'camp_id' and represents the combo box???
23e4fc3add5443f51e55663cfc245d24?d=identicon&s=25 Erol Fornoles (Guest)
on 2008-10-10 12:20
(Received via mailing list)
On Oct 10, 6:04 pm, Jay Pangmi <rails-mailing-l...@andreas-s.net>
wrote:
> Thanks a tonn man... it works sweet. So, :camp, :id in the partial
> becomes 'camp_id' and represents the combo box???
> --
> Posted viahttp://www.ruby-forum.com/.

Yeah, select :camp, :id generates a select tag with an id of "camp_id"
and a name of "camp[id]".
This topic is locked and can not be replied to.