I''ve got a select box on a page, and when a user chooses a value in the select box, certain elements of the page should change. I don''t want to make them push a submit button, the stuff should just change automatically. I figured I could use Ajax to do this, but I don''t know how to make the Ajax code run in the select box''s onChange() handler. I took a look at the backpackit source, and the Ajax code runs in the checkboxes'' onclick handler. How can I make the Ajax code run onchange()? Thanks
There is a (undocumented) helper function called "remote_function (options)". Use it like this: ... onchange="<%=remote_function(:update => "dom_id", :url => { :action => "action" })%>" ... options is a hash with the same syntax as the one on link_to_remote. Haven''t tested this but should work ok. -- Thomas Am 11.05.2005 um 08:20 schrieb Pat Maddox:> I''ve got a select box on a page, and when a user chooses a value in > the select box, certain elements of the page should change. I don''t > want to make them push a submit button, the stuff should just change > automatically. I figured I could use Ajax to do this, but I don''t > know how to make the Ajax code run in the select box''s onChange() > handler. I took a look at the backpackit source, and the Ajax code > runs in the checkboxes'' onclick handler. How can I make the Ajax code > run onchange()? > > Thanks > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
That doesn''t seem to pass the value of the select box. How can I get the value? On 5/11/05, Thomas Fuchs <thomas-9D208sng4xU@public.gmane.org> wrote:> There is a (undocumented) helper function called "remote_function > (options)". > > Use it like this: > > ... onchange="<%=remote_function(:update => "dom_id", :url => > { :action => "action" })%>" ... > > options is a hash with the same syntax as the one on link_to_remote. > > Haven''t tested this but should work ok. > > -- > Thomas > > Am 11.05.2005 um 08:20 schrieb Pat Maddox: > > > I''ve got a select box on a page, and when a user chooses a value in > > the select box, certain elements of the page should change. I don''t > > want to make them push a submit button, the stuff should just change > > automatically. I figured I could use Ajax to do this, but I don''t > > know how to make the Ajax code run in the select box''s onChange() > > handler. I took a look at the backpackit source, and the Ajax code > > runs in the checkboxes'' onclick handler. How can I make the Ajax code > > run onchange()? > > > > Thanks > > _______________________________________________ > > Rails mailing list > > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > > http://lists.rubyonrails.org/mailman/listinfo/rails > > > >
I need to get a couple pieces of data from this. I tried doing @price = ... @inventory = ... But if I try to access @price or @inventory from within the javascript to update the page, they''re just both blank. How can I pass that data back to the page as well? On 5/11/05, Thomas Fuchs <thomas-9D208sng4xU@public.gmane.org> wrote:> There is a (undocumented) helper function called "remote_function > (options)". > > Use it like this: > > ... onchange="<%=remote_function(:update => "dom_id", :url => > { :action => "action" })%>" ... > > options is a hash with the same syntax as the one on link_to_remote. > > Haven''t tested this but should work ok. > > -- > Thomas > > Am 11.05.2005 um 08:20 schrieb Pat Maddox: > > > I''ve got a select box on a page, and when a user chooses a value in > > the select box, certain elements of the page should change. I don''t > > want to make them push a submit button, the stuff should just change > > automatically. I figured I could use Ajax to do this, but I don''t > > know how to make the Ajax code run in the select box''s onChange() > > handler. I took a look at the backpackit source, and the Ajax code > > runs in the checkboxes'' onclick handler. How can I make the Ajax code > > run onchange()? > > > > Thanks > > _______________________________________________ > > Rails mailing list > > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > > http://lists.rubyonrails.org/mailman/listinfo/rails > > > >
You might also be able to use the observe_field helper - detailed in the wiki here: http://wiki.rubyonrails.com/rails/show/How%20to%20make%20a%20real-time%20search%20box%20with%20the%20Ajax%20helpers Or in the API doc here: http://api.rubyonrails.com/classes/ActionView/Helpers/JavascriptHelper.html#M000369 observe_field looks for a single field to of changed, observe_form for a whole form. Bear in mind that these observe_x calls poll the field(s) being observed using Javascript, so using it for a number of controls might be slow, not to mention the fact that you''ll end up with a lot of Javascript fragments in your code. sam On 5/11/05, Pat Maddox <pergesu-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I need to get a couple pieces of data from this. I tried doing > @price = ... > @inventory = ... > > But if I try to access @price or @inventory from within the javascript > to update the page, they''re just both blank. How can I pass that data > back to the page as well? > > > On 5/11/05, Thomas Fuchs <thomas-9D208sng4xU@public.gmane.org> wrote: > > There is a (undocumented) helper function called "remote_function > > (options)". > > > > Use it like this: > > > > ... onchange="<%=remote_function(:update => "dom_id", :url => > > { :action => "action" })%>" ... > > > > options is a hash with the same syntax as the one on link_to_remote. > > > > Haven''t tested this but should work ok. > > > > -- > > Thomas > > > > Am 11.05.2005 um 08:20 schrieb Pat Maddox: > > > > > I''ve got a select box on a page, and when a user chooses a value in > > > the select box, certain elements of the page should change. I don''t > > > want to make them push a submit button, the stuff should just change > > > automatically. I figured I could use Ajax to do this, but I don''t > > > know how to make the Ajax code run in the select box''s onChange() > > > handler. I took a look at the backpackit source, and the Ajax code > > > runs in the checkboxes'' onclick handler. How can I make the Ajax code > > > run onchange()? > > > > > > Thanks > > > _______________________________________________ > > > Rails mailing list > > > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > > > http://lists.rubyonrails.org/mailman/listinfo/rails > > > > > > > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-- sam http://www.magpiebrain.com/
Let me show you guys what I''m trying to do: http://hemp.dnswatchdog.com/item/show/715 When the user selects a size, the price and "in stock" should be updated with values from the db. This would be really simple "throw it in a remote form" stuff if that option weren''t also part of the form when they add the item to their cart. I have to somehow figure out how to update those values. I''ve already got a partial written and everything, the only thing left to figure out is to make the ajax call and pass the value of the select box. I''ve been working on this one thing for over five hours now...getting extremely frustrated. I''d appreciate any help in figuring this out. Pat On 5/11/05, Sam Newman <sam.newman-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> You might also be able to use the observe_field helper - detailed in > the wiki here: http://wiki.rubyonrails.com/rails/show/How%20to%20make%20a%20real-time%20search%20box%20with%20the%20Ajax%20helpers > > Or in the API doc here: > http://api.rubyonrails.com/classes/ActionView/Helpers/JavascriptHelper.html#M000369 > > observe_field looks for a single field to of changed, observe_form for > a whole form. > > Bear in mind that these observe_x calls poll the field(s) being > observed using Javascript, so using it for a number of controls might > be slow, not to mention the fact that you''ll end up with a lot of > Javascript fragments in your code. > > sam > > On 5/11/05, Pat Maddox <pergesu-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > I need to get a couple pieces of data from this. I tried doing > > @price = ... > > @inventory = ... > > > > But if I try to access @price or @inventory from within the javascript > > to update the page, they''re just both blank. How can I pass that data > > back to the page as well? > > > > > > On 5/11/05, Thomas Fuchs <thomas-9D208sng4xU@public.gmane.org> wrote: > > > There is a (undocumented) helper function called "remote_function > > > (options)". > > > > > > Use it like this: > > > > > > ... onchange="<%=remote_function(:update => "dom_id", :url => > > > { :action => "action" })%>" ... > > > > > > options is a hash with the same syntax as the one on link_to_remote. > > > > > > Haven''t tested this but should work ok. > > > > > > -- > > > Thomas > > > > > > Am 11.05.2005 um 08:20 schrieb Pat Maddox: > > > > > > > I''ve got a select box on a page, and when a user chooses a value in > > > > the select box, certain elements of the page should change. I don''t > > > > want to make them push a submit button, the stuff should just change > > > > automatically. I figured I could use Ajax to do this, but I don''t > > > > know how to make the Ajax code run in the select box''s onChange() > > > > handler. I took a look at the backpackit source, and the Ajax code > > > > runs in the checkboxes'' onclick handler. How can I make the Ajax code > > > > run onchange()? > > > > > > > > Thanks > > > > _______________________________________________ > > > > Rails mailing list > > > > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > > > > http://lists.rubyonrails.org/mailman/listinfo/rails > > > > > > > > > > > > _______________________________________________ > > Rails mailing list > > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > > http://lists.rubyonrails.org/mailman/listinfo/rails > > > > -- > sam > http://www.magpiebrain.com/ >
I don't really like the way observe_field works. Maybe its zippier outside of Webrick, but it can introduce some weird lags… anyways. what you want is something like this: remote_function(:update => 'dom_id', :url => {:action => 'your_action'}, :with => "'this_fields_new_value=' + escape(value) +'&other_value=#{@one_of_your_session_variables}'") I don't think its officially outlined, but i took a look into the actual javascript as well as the helper code and… well it works. why isn't remote_function even documented on the API pages? Or is it… i've been using a local copy of it since last week… i had to poke through some source to find it Anyways, hope this helps On 5/11/05, Pat Maddox <pergesu@gmail.com> wrote:> Let me show you guys what I'm trying to do: > > http://hemp.dnswatchdog.com/item/show/715 > > When the user selects a size, the price and "in stock" should be > updated with values from the db. This would be really simple "throw > it in a remote form" stuff if that option weren't also part of the > form when they add the item to their cart. I have to somehow figure > out how to update those values. I've already got a partial written > and everything, the only thing left to figure out is to make the ajax > call and pass the value of the select box. > > I've been working on this one thing for over five hours now...getting > extremely frustrated. I'd appreciate any help in figuring this out. > > Pat > > > On 5/11/05, Sam Newman <sam.newman@gmail.com> wrote: > > You might also be able to use the observe_field helper - detailed in > > the wiki here: http://wiki.rubyonrails.com/rails/show/How%20to%20make%20a%20real-time%20search%20box%20with%20the%20Ajax%20helpers > > > > Or in the API doc here: > > http://api.rubyonrails.com/classes/ActionView/Helpers/JavascriptHelper.html#M000369 > > > > observe_field looks for a single field to of changed, observe_form for > > a whole form. > > > > Bear in mind that these observe_x calls poll the field(s) being > > observed using Javascript, so using it for a number of controls might > > be slow, not to mention the fact that you'll end up with a lot of > > Javascript fragments in your code. > > > > sam > > > > On 5/11/05, Pat Maddox <pergesu@gmail.com> wrote: > > > I need to get a couple pieces of data from this. I tried doing > > > @price = ... > > > @inventory = ... > > > > > > But if I try to access @price or @inventory from within the javascript > > > to update the page, they're just both blank. How can I pass that data > > > back to the page as well? > > > > > > > > > On 5/11/05, Thomas Fuchs <thomas@fesch.at> wrote: > > > > There is a (undocumented) helper function called "remote_function > > > > (options)". > > > > > > > > Use it like this: > > > > > > > > ... onchange="<%=remote_function(:update => "dom_id", :url => > > > > { :action => "action" })%>" ... > > > > > > > > options is a hash with the same syntax as the one on link_to_remote. > > > > > > > > Haven't tested this but should work ok. > > > > > > > > -- > > > > Thomas > > > > > > > > Am 11.05.2005 um 08:20 schrieb Pat Maddox: > > > > > > > > > I've got a select box on a page, and when a user chooses a value in > > > > > the select box, certain elements of the page should change. I don't > > > > > want to make them push a submit button, the stuff should just change > > > > > automatically. I figured I could use Ajax to do this, but I don't > > > > > know how to make the Ajax code run in the select box's onChange() > > > > > handler. I took a look at the backpackit source, and the Ajax code > > > > > runs in the checkboxes' onclick handler. How can I make the Ajax code > > > > > run onchange()? > > > > > > > > > > Thanks > > > > > _______________________________________________ > > > > > Rails mailing list > > > > > Rails@lists.rubyonrails.org > > > > > http://lists.rubyonrails.org/mailman/listinfo/rails > > > > > > > > > > > > > > > > _______________________________________________ > > > Rails mailing list > > > Rails@lists.rubyonrails.org > > > http://lists.rubyonrails.org/mailman/listinfo/rails > > > > > > > -- > > sam > > http://www.magpiebrain.com/ > > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >_______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
Pat, I''d suggest that if Rails doesn''t give you the helper you need, the observe_field helper doesn''t fit your needs, and this is basically just a one-off, then throw in the Javascript yourself. It''s quite easy, and easier to read than the remote_function call (but then again, I haven''t done more than "hello world" in rails, so maybe it''s something you learn to read easier later): <select onselect="new Ajax.Updater(''targetdiv'', ''/thing/view/'' + this.options[this.selectedIndex].text)"> I''m pretty sure prototype''s Ajax is async by default, so that''s all you need to give in your javascript. Steve On 5/11/05, Pat Maddox <pergesu-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Let me show you guys what I''m trying to do: > > http://hemp.dnswatchdog.com/item/show/715 > > When the user selects a size, the price and "in stock" should be > updated with values from the db. This would be really simple "throw > it in a remote form" stuff if that option weren''t also part of the > form when they add the item to their cart. I have to somehow figure > out how to update those values. I''ve already got a partial written > and everything, the only thing left to figure out is to make the ajax > call and pass the value of the select box. > > I''ve been working on this one thing for over five hours now...getting > extremely frustrated. I''d appreciate any help in figuring this out. > > Pat > > > On 5/11/05, Sam Newman <sam.newman-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > You might also be able to use the observe_field helper - detailed in > > the wiki here: http://wiki.rubyonrails.com/rails/show/How%20to%20make%20a%20real-time%20search%20box%20with%20the%20Ajax%20helpers > > > > Or in the API doc here: > > http://api.rubyonrails.com/classes/ActionView/Helpers/JavascriptHelper.html#M000369 > > > > observe_field looks for a single field to of changed, observe_form for > > a whole form. > > > > Bear in mind that these observe_x calls poll the field(s) being > > observed using Javascript, so using it for a number of controls might > > be slow, not to mention the fact that you''ll end up with a lot of > > Javascript fragments in your code. > > > > sam > > > > On 5/11/05, Pat Maddox <pergesu-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > I need to get a couple pieces of data from this. I tried doing > > > @price = ... > > > @inventory = ... > > > > > > But if I try to access @price or @inventory from within the javascript > > > to update the page, they''re just both blank. How can I pass that data > > > back to the page as well? > > > > > > > > > On 5/11/05, Thomas Fuchs <thomas-9D208sng4xU@public.gmane.org> wrote: > > > > There is a (undocumented) helper function called "remote_function > > > > (options)". > > > > > > > > Use it like this: > > > > > > > > ... onchange="<%=remote_function(:update => "dom_id", :url => > > > > { :action => "action" })%>" ... > > > > > > > > options is a hash with the same syntax as the one on link_to_remote. > > > > > > > > Haven''t tested this but should work ok. > > > > > > > > -- > > > > Thomas > > > > > > > > Am 11.05.2005 um 08:20 schrieb Pat Maddox: > > > > > > > > > I''ve got a select box on a page, and when a user chooses a value in > > > > > the select box, certain elements of the page should change. I don''t > > > > > want to make them push a submit button, the stuff should just change > > > > > automatically. I figured I could use Ajax to do this, but I don''t > > > > > know how to make the Ajax code run in the select box''s onChange() > > > > > handler. I took a look at the backpackit source, and the Ajax code > > > > > runs in the checkboxes'' onclick handler. How can I make the Ajax code > > > > > run onchange()? > > > > > > > > > > Thanks > > > > > _______________________________________________ > > > > > Rails mailing list > > > > > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > > > > > http://lists.rubyonrails.org/mailman/listinfo/rails > > > > > > > > > > > > > > > > _______________________________________________ > > > Rails mailing list > > > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > > > http://lists.rubyonrails.org/mailman/listinfo/rails > > > > > > > -- > > sam > > http://www.magpiebrain.com/ > > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
That''s actually exactly what I ended up doing. Just made a couple method calls to see what format the javascript was in, and then I copied and pasted it directly into my code, using ruby to make some of the things dynamic. Not the prettiest way of doing things, but it works. On 5/11/05, Steve Willer <steve.willer-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Pat, I''d suggest that if Rails doesn''t give you the helper you need, > the observe_field helper doesn''t fit your needs, and this is basically > just a one-off, then throw in the Javascript yourself. It''s quite > easy, and easier to read than the remote_function call (but then > again, I haven''t done more than "hello world" in rails, so maybe it''s > something you learn to read easier later): > > <select onselect="new Ajax.Updater(''targetdiv'', ''/thing/view/'' + > this.options[this.selectedIndex].text)"> > > I''m pretty sure prototype''s Ajax is async by default, so that''s all > you need to give in your javascript. > > > Steve > > On 5/11/05, Pat Maddox <pergesu-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > Let me show you guys what I''m trying to do: > > > > http://hemp.dnswatchdog.com/item/show/715 > > > > When the user selects a size, the price and "in stock" should be > > updated with values from the db. This would be really simple "throw > > it in a remote form" stuff if that option weren''t also part of the > > form when they add the item to their cart. I have to somehow figure > > out how to update those values. I''ve already got a partial written > > and everything, the only thing left to figure out is to make the ajax > > call and pass the value of the select box. > > > > I''ve been working on this one thing for over five hours now...getting > > extremely frustrated. I''d appreciate any help in figuring this out. > > > > Pat > > > > > > On 5/11/05, Sam Newman <sam.newman-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > You might also be able to use the observe_field helper - detailed in > > > the wiki here: http://wiki.rubyonrails.com/rails/show/How%20to%20make%20a%20real-time%20search%20box%20with%20the%20Ajax%20helpers > > > > > > Or in the API doc here: > > > http://api.rubyonrails.com/classes/ActionView/Helpers/JavascriptHelper.html#M000369 > > > > > > observe_field looks for a single field to of changed, observe_form for > > > a whole form. > > > > > > Bear in mind that these observe_x calls poll the field(s) being > > > observed using Javascript, so using it for a number of controls might > > > be slow, not to mention the fact that you''ll end up with a lot of > > > Javascript fragments in your code. > > > > > > sam > > > > > > On 5/11/05, Pat Maddox <pergesu-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > I need to get a couple pieces of data from this. I tried doing > > > > @price = ... > > > > @inventory = ... > > > > > > > > But if I try to access @price or @inventory from within the javascript > > > > to update the page, they''re just both blank. How can I pass that data > > > > back to the page as well? > > > > > > > > > > > > On 5/11/05, Thomas Fuchs <thomas-9D208sng4xU@public.gmane.org> wrote: > > > > > There is a (undocumented) helper function called "remote_function > > > > > (options)". > > > > > > > > > > Use it like this: > > > > > > > > > > ... onchange="<%=remote_function(:update => "dom_id", :url => > > > > > { :action => "action" })%>" ... > > > > > > > > > > options is a hash with the same syntax as the one on link_to_remote. > > > > > > > > > > Haven''t tested this but should work ok. > > > > > > > > > > -- > > > > > Thomas > > > > > > > > > > Am 11.05.2005 um 08:20 schrieb Pat Maddox: > > > > > > > > > > > I''ve got a select box on a page, and when a user chooses a value in > > > > > > the select box, certain elements of the page should change. I don''t > > > > > > want to make them push a submit button, the stuff should just change > > > > > > automatically. I figured I could use Ajax to do this, but I don''t > > > > > > know how to make the Ajax code run in the select box''s onChange() > > > > > > handler. I took a look at the backpackit source, and the Ajax code > > > > > > runs in the checkboxes'' onclick handler. How can I make the Ajax code > > > > > > run onchange()? > > > > > > > > > > > > Thanks > > > > > > _______________________________________________ > > > > > > Rails mailing list > > > > > > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > > > > > > http://lists.rubyonrails.org/mailman/listinfo/rails > > > > > > > > > > > > > > > > > > > > _______________________________________________ > > > > Rails mailing list > > > > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > > > > http://lists.rubyonrails.org/mailman/listinfo/rails > > > > > > > > > > -- > > > sam > > > http://www.magpiebrain.com/ > > > > > _______________________________________________ > > Rails mailing list > > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > > http://lists.rubyonrails.org/mailman/listinfo/rails > > >
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Pat Maddox wrote:> That doesn''t seem to pass the value of the select box. How can I get the value?I tried to do the exact same thing... the problem is that the form element needs to be serialized into the request. One way, as you found, is to write the javascript yourself. I ended up with a complicated helper call that may not be any easier, but it handles the creation of the select tag and options, as well as sets the proper action: <%= collection_select(''plan'', ''name'', @plans, ''id'', ''name'', {:include_blank => true}, {:onchange => remote_function( :url => { :action => "get_plan_with_ajax", :id => @customer.id}, :with => "Form.Element.serialize(this)", :method => "''post''", :loading => ''$(\''mem_ajax_status\'').innerHTML = \''loading ...\'''', :complete => ''eval(request.responseText);$(\''mem_ajax_status\'').innerHTML = \'' \'''' )}) %> Notice that I had to add the :with => "Form.Element.serialize(this)" in order to get the form elements added. The eval line in the :complete part is using my send_ajax helper in ticket #933 , but is not necessary for your example. - -- David Morton Maia Mailguard server side anti-spam/anti-virus solution: http://www.maiamailguard.com -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.2.5 (GNU/Linux) Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org iD8DBQFCg4PcSIxC85HZHLMRAmMNAKCmu2o2s2rcORgCzY8hycejF59SlQCfY8XW imrv1ohGaG75JrJftt3DcaE=2bo5 -----END PGP SIGNATURE-----