Pretty new to prototype and I am having trouble setting the style on multiple elements. Let''s say I have 20 divs with individual ids. I can set the style of the current div easily with Element.setStyle(''div12'', {''font-weight'':''bold''} ); but if I click on another div, I want to change div12 back to ''font- weight'':''normal'' and the new div to bold. I have tried different iterations of .each and .invoke by placing the ids into a js array, for example $rowid.each(Element.setStyle,{''font-weight'':''normal''}); or $rowid.invoke(''setStyle'', ''font-weight'':''normal''}); neither of them work. I don''t know if this is possible with setStyle, but I have used it successfully with $jsid.each(Element.hide); --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
Hi Chris, I think you need to possibly first take your ids so you have an array of elements $rowids.map($).invoke(''setStyle'', { ''fontWeight'':''normal'' }); As far as I know, the invoke calls the first-parameter-method on each item in the set, so you have to first take your ids and extend them into an array of elements, or you''re trying to call the method on a string. The map calls the $ function for each item in the string (used as an iterator) and then returns the result set, so its the same as doing $(''id1'',''id2'',''id3'') intially, I just took your example with $rowids Hope this helps, I haven''t checked it though.. never enough hours in the day. Gareth On 9/18/07, ChrisM <meiering-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > > Pretty new to prototype and I am having trouble setting the style on > multiple elements. > > Let''s say I have 20 divs with individual ids. I can set the style of > the current div easily with > > Element.setStyle(''div12'', {''font-weight'':''bold''} ); > > but if I click on another div, I want to change div12 back to ''font- > weight'':''normal'' and the new div to bold. > > I have tried different iterations of .each and .invoke by placing the > ids into a js array, for example > $rowid.each(Element.setStyle,{''font-weight'':''normal''}); > or > $rowid.invoke(''setStyle'', ''font-weight'':''normal''}); > > neither of them work. I don''t know if this is possible with setStyle, > but I have used it successfully with > $jsid.each(Element.hide); > > > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
First, it''s not a good idea to use ids for presentational purposes : ) Classes should do just fine and is much easier to work with $$(''.someClass'').invoke(''setStyle'', {background: ''#f00''}) If you still decide to use ids, it gets a bit more verbose: $R(1,12).each(function(n){$(''row'' + n).setStyle({color: ''#ddf''})}) or the way Gareth showed with mapping $ to each array''s element. You can see that it''s much uglier solution, so once again, I suggest to stick with classes and you''ll be just fine. Hope this helps. Best, kangax -- View this message in context: http://www.nabble.com/setStyle-on-multiple-elements-tf4470826.html#a12748931 Sent from the RubyOnRails Spinoffs mailing list archive at Nabble.com. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
Gareth, Thanks for the prompt reply and for letting me know about .map($). I was using PHP to format the JS element ($rowid) from a PHP array and had an error in that code. This ended up working: $rowid = $(\''row''.implode("'', ''row", $ids).''\''); $rowid.invoke(\''setStyle\'',{\''fontWeight\'':\''normal\'', \''backgroundColor\'':\''#fff7cb\''}); Cheers, Chris On Sep 17, 5:19 pm, "Gareth Evans" <agr...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hi Chris, > > I think you need to possibly first take your ids so you have an array of > elements > > $rowids.map($).invoke(''setStyle'', { ''fontWeight'':''normal'' }); > > As far as I know, the invoke calls the first-parameter-method on each item > in the set, so you have to first take your ids and extend them into an array > of elements, or you''re trying to call the method on a string. > > The map calls the $ function for each item in the string (used as an > iterator) and then returns the result set, so its the same as doing > $(''id1'',''id2'',''id3'') intially, I just took your example with $rowids > > Hope this helps, I haven''t checked it though.. never enough hours in the > day. > > Gareth > > On 9/18/07, ChrisM <meier...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > Pretty new to prototype and I am having trouble setting the style on > > multiple elements. > > > Let''s say I have 20 divs with individual ids. I can set the style of > > the current div easily with > > > Element.setStyle(''div12'', {''font-weight'':''bold''} ); > > > but if I click on another div, I want to change div12 back to ''font- > > weight'':''normal'' and the new div to bold. > > > I have tried different iterations of .each and .invoke by placing the > > ids into a js array, for example > > $rowid.each(Element.setStyle,{''font-weight'':''normal''}); > > or > > $rowid.invoke(''setStyle'', ''font-weight'':''normal''}); > > > neither of them work. I don''t know if this is possible with setStyle, > > but I have used it successfully with > > $jsid.each(Element.hide);--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
Kangax, Thanks for this recommendation. I wasn''t thinking in this direction, but it will be much cleaner. Is there any downside to using something like $$(''.class'').invoke(''removeClassName'',''active'') with this.addClassName(''active'') Thanks again, Chris On Sep 17, 7:37 pm, kangax <kan...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> First, it''s not a good idea to use ids for presentational purposes : ) > Classes should do just fine and is much easier to work with > $$(''.someClass'').invoke(''setStyle'', {background: ''#f00''}) > > If you still decide to use ids, it gets a bit more verbose: > > $R(1,12).each(function(n){$(''row'' + n).setStyle({color: ''#ddf''})}) > > or the way Gareth showed with mapping $ to each array''s element. > > You can see that it''s much uglier solution, so once again, I suggest to > stick with classes and you''ll be just fine. > > Hope this helps. > > Best, > kangax > -- > View this message in context:http://www.nabble.com/setStyle-on-multiple-elements-tf4470826.html#a1... > Sent from the RubyOnRails Spinoffs mailing list archive at Nabble.com.On Sep 17, 7:37 pm, kangax <kan...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> First, it''s not a good idea to use ids for presentational purposes : ) > Classes should do just fine and is much easier to work with > $$(''.someClass'').invoke(''setStyle'', {background: ''#f00''}) > > If you still decide to use ids, it gets a bit more verbose: > > $R(1,12).each(function(n){$(''row'' + n).setStyle({color: ''#ddf''})}) > > or the way Gareth showed with mapping $ to each array''s element. > > You can see that it''s much uglier solution, so once again, I suggest to > stick with classes and you''ll be just fine. > > Hope this helps. > > Best, > kangax > -- > View this message in context:http://www.nabble.com/setStyle-on-multiple-elements-tf4470826.html#a1... > Sent from the RubyOnRails Spinoffs mailing list archive at Nabble.com.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
Kangax, Thanks for this recommendation. I wasn''t thinking in this direction, but it will be much cleaner. Is there any downside to using something like $$(''.class'').invoke(''removeClassName'',''active'') with this.addClassName(''active'') Thanks again, Chris On Sep 17, 7:37 pm, kangax <kan...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> First, it''s not a good idea to use ids for presentational purposes : ) > Classes should do just fine and is much easier to work with > $$(''.someClass'').invoke(''setStyle'', {background: ''#f00''}) > > If you still decide to use ids, it gets a bit more verbose: > > $R(1,12).each(function(n){$(''row'' + n).setStyle({color: ''#ddf''})}) > > or the way Gareth showed with mapping $ to each array''s element. > > You can see that it''s much uglier solution, so once again, I suggest to > stick with classes and you''ll be just fine. > > Hope this helps. > > Best, > kangax > -- > View this message in context:http://www.nabble.com/setStyle-on-multiple-elements-tf4470826.html#a1... > Sent from the RubyOnRails Spinoffs mailing list archive at Nabble.com.On Sep 17, 7:37 pm, kangax <kan...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> First, it''s not a good idea to use ids for presentational purposes : ) > Classes should do just fine and is much easier to work with > $$(''.someClass'').invoke(''setStyle'', {background: ''#f00''}) > > If you still decide to use ids, it gets a bit more verbose: > > $R(1,12).each(function(n){$(''row'' + n).setStyle({color: ''#ddf''})}) > > or the way Gareth showed with mapping $ to each array''s element. > > You can see that it''s much uglier solution, so once again, I suggest to > stick with classes and you''ll be just fine. > > Hope this helps. > > Best, > kangax > -- > View this message in context:http://www.nabble.com/setStyle-on-multiple-elements-tf4470826.html#a1... > Sent from the RubyOnRails Spinoffs mailing list archive at Nabble.com.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---