Hi, I want to add a dropdown selection box that when an item is selected a javascript function gets called, is there an easy way to do this with Rails? Thanks! -- Kind Regards, Rajinder Yadav SafetyNet Test Driven Development http://safetynet.devmentor.org -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On Tue, Feb 28, 2012 at 16:50, Dev Guy <devguy.ca-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I want to add a dropdown selection box that when an item is selected a > javascript function gets called, is there an easy way to do this with > Rails?Sure, though it''s not Rails-specific; just use the onchange attribute. See: http://www.w3schools.com/jsref/event_onchange.asp for an intro to the concept. -Dave -- Dave Aronson: Available Cleared Ruby on Rails Freelancer (NoVa/DC/Remote) -- see www.DaveAronson.com, and blogs at www.Codosaur.us, www.Dare2XL.com, www.RecruitingRants.com -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
There are a few ways to do this. If you are looking for an ajax response you can do the following: (this is an example with a new action) <%= form_for Model.new, :remote => true do |f| %> <div>Your Label</div> <div> <%= f.collection_select(:some_id, Model.all, :id, :name, :class => ''yourclass'' %> </div> <% end %> Then in your javascript do something similar to: $(''select#model_some_id'').bind({ click:function () { // do something when it''s clicked }, keydown:function () { // do something when a key is pressed down in the select }, keyup:function () { // do something when a key is pressed up in the select } }); This handles most of the browser events you''ll find in Mozilla, IE, Safari, etc. as far as handling events. Some change events are not noticed by Mozilla (for instance if someone is using drop down arrows while in a select field). There are quite a few ajax tutorials out there. -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On Wed, Feb 29, 2012 at 11:21 PM, Alpha Blue <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> There are a few ways to do this. If you are looking for an ajax > response you can do the following: > > (this is an example with a new action) > > <%= form_for Model.new, :remote => true do |f| %> > > <div>Your Label</div> > <div> > <%= f.collection_select(:some_id, Model.all, :id, :name, :class => > ''yourclass'' %> > </div> > > <% end %> > > Then in your javascript do something similar to: > > $(''select#model_some_id'').bind({ > click:function () { > // do something when it''s clicked > }, > keydown:function () { > // do something when a key is pressed down in the select > }, > keyup:function () { > // do something when a key is pressed up in the select > } > }); > > This handles most of the browser events you''ll find in Mozilla, IE, > Safari, etc. as far as handling events. Some change events are not > noticed by Mozilla (for instance if someone is using drop down arrows > while in a select field). > > There are quite a few ajax tutorials out there. > > -- > Posted via http://www.ruby-forum.com/. > > -- > You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. > For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en. >Hi thanks, this is exactly what I was looking for, a solution using jQuery to bind a function. Still green on ajax, so your example was very useful and also the link provided above for a javescript refresher on events. -- Kind Regards, Rajinder Yadav SafetyNet Test Driven Development http://safetynet.devmentor.org -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.