Greetings all,
I''ve run into something in prototype.js (1.5.0) that I''ve not
been
able to solve given a reasonable amount of work this morning.  I''ve
got to move onto something else, but I thought I''d send out a cry for
help and see if any of you could suggest a solution (or even a better
way of doing things!)
So, I''ve been working on a site based off of this template:
http://www.oswd.org/design/preview/id/2985.
As you can see from the template, there''s a horizontal menu of tabs on
the top of the content area.  While I like the technique the author
used to make the list items forming the individual tabs look rounded,
I don''t like that clicking on the tab doesn''t take you to the
page
you''re looking for -- only clicking on the text of the tab works.
Now, I know I could probably remove the list altogether and use
styling to make the anchor tags look like tabs, but, for the moment, I
thought I''d try to try and enhance my understanding of prototype by
solving the problem in javascript.  It probably won''t be the final
solution, just a way for me to learn a little bit more.
Anyway, I was successfully able to use the each() method in
conjunction with $$() to get the tabs and then apply a click event
observer which would get the href of the enclosed link and set
window.location.href accordingly.  However, it seems to me that, since
all the each() method was doing was calling a function I named
link_from_tab(), that the invoke() method seemed to fit the bill
better then each().
Here''s the code that worked:
function link_from_tab(tab) { Event.observe(tab, "click", function()
{ window.location.href = tab.firstChild.href }); }
Event.observe(window, "load", function() { $$("ul#tabs
li").each(function(tab) { link_from_tab(tab) })});
But, here''s what I tried to change the second line to:
Event.observe(window, "load", function() { $$("ul#tabs
li").invoke(link_from_tab) })
Which didn''t work because the "tab" parameter wasn''t
passed to the
link_from_tab function.  The next step was to try:
Event.observe(window, "load", function() { $$("ul#tabs
li").invoke(link_from_tab, this) })
But that also didn''t seem to work.
Thus, how can I pass the current element within the $$("ul#tabs li")
array to the function called by the invoke method?
Thanks, all,
 -- Dash --
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---
Dash,> Event.observe(window, "load", function() { $$("ul#tabs > li").invoke(link_from_tab) }) > > Which didn''t work because the "tab" parameter wasn''t passed to the > link_from_tab function. The next step was to try: >invoke(''method'') is here to call objects method. Example taken from the doc : [''hello'', ''world'', ''cool!''].invoke(''toUpperCase'') The method ''toUpperCase'' will be called for all elements wich *are strings*. toUpperCase is a method of String. However link_from_tab is not a method of Element. You should use map() instead : $$("ul#tabs li").map(link_from_tab) Yours, Nicolas Terray --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Using "each" will also work:
$$("ul#tabs li").each(link_from_tab);
Cheers,
Andrew
On Mar 1, 7:49 am, "Nicolas Terray"
<nicolas.ter...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
wrote:> Dash,
>
> > Event.observe(window, "load", function() { $$("ul#tabs
> > li").invoke(link_from_tab) })
>
> > Which didn''t work because the "tab" parameter
wasn''t passed to the
> > link_from_tab function.  The next step was to try:
>
> invoke(''method'') is here to call objects method. Example
taken from the doc :
> [''hello'', ''world'',
''cool!''].invoke(''toUpperCase'')
> The method ''toUpperCase'' will be called for all elements
wich *are
> strings*. toUpperCase is a method of String. However link_from_tab is
> not a method of Element.
>
> You should use map() instead :
> $$("ul#tabs li").map(link_from_tab)
>
> Yours,
> Nicolas Terray
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---
Each is what I was using. I missed that invoke() was for methods and wanted to avoid having the inline function created within each. Thanks for your help! -- Dave -- Andrew Dupont wrote:> Using "each" will also work: > > $$("ul#tabs li").each(link_from_tab); > > Cheers, > Andrew > > On Mar 1, 7:49 am, "Nicolas Terray" <nicolas.ter...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > >> Dash, >> >> >>> Event.observe(window, "load", function() { $$("ul#tabs >>> li").invoke(link_from_tab) }) >>> >>> Which didn''t work because the "tab" parameter wasn''t passed to the >>> link_from_tab function. The next step was to try: >>> >> invoke(''method'') is here to call objects method. Example taken from the doc : >> [''hello'', ''world'', ''cool!''].invoke(''toUpperCase'') >> The method ''toUpperCase'' will be called for all elements wich *are >> strings*. toUpperCase is a method of String. However link_from_tab is >> not a method of Element. >> >> You should use map() instead : >> $$("ul#tabs li").map(link_from_tab) >> >> Yours, >> Nicolas Terray >> > > > > > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---