smartcookie
2008-May-01 00:14 UTC
finding the index of the selected item in getElementByclassname
I have a question: How do you return the selected index of an array of elementsbyclassname on the element that execute an onclick event? lets say : <a class="link" onclick="myfunction(this)"></a> <a class="link" onclick="myfunction(this)"></a> <a class="link" onclick="myfunction(this)"></a> Thanks --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Justin Perkins
2008-May-01 02:27 UTC
Re: finding the index of the selected item in getElementByclassname
On Wed, Apr 30, 2008 at 7:14 PM, smartcookie <dpell.home-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> How do you return the selected index of an array of > elementsbyclassname on the element that execute an onclick event?First thing is you should use select() instead of getElementsByClassName(), as you will run into problems with browsers that support the latter natively such as Firefox3. Here are some examples... $$(''a.link'').invoke(''onclick''); // call the onclick event for every link with a class of "link" on the page $$(''html body'').select(''a.link'').first().onclick(); // call the onclick event for the first link with a class of "link" on the page $$(''html body'').select(''a.link'')[5].onclick(); // call the onclick event for the 6th link on the page with a class of "link" -justin --~--~---------~--~----~------------~-------~--~----~ 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
2008-May-01 03:34 UTC
Re: finding the index of the selected item in getElementByclassname
var links = $$(''.link''); links.invoke(''observe'', ''click'', function(e) { console.log(links.indexOf(e.currentTarget)); // will log index of clicked element }) - kangax On Apr 30, 8:14 pm, smartcookie <dpell.h...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I have a question: > > How do you return the selected index of an array of > elementsbyclassname on the element that execute an onclick event? > > lets say : > > <a class="link" onclick="myfunction(this)"></a> > <a class="link" onclick="myfunction(this)"></a> > <a class="link" onclick="myfunction(this)"></a> > > Thanks--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
smartcookie
2008-May-02 01:58 UTC
Re: finding the index of the selected item in getElementByclassname
Thanks! cool. I was doing this stupid loop with a conditional statement On Apr 30, 11:34 pm, kangax <kan...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> var links = $$(''.link''); > links.invoke(''observe'', ''click'', function(e) { > console.log(links.indexOf(e.currentTarget)); // will log index of > clicked element > > }) > > - kangax > > On Apr 30, 8:14 pm, smartcookie <dpell.h...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > I have a question: > > > How do you return the selected index of an array of > > elementsbyclassname on the element that execute an onclick event? > > > lets say : > > > <a class="link" onclick="myfunction(this)"></a> > > <a class="link" onclick="myfunction(this)"></a> > > <a class="link" onclick="myfunction(this)"></a> > > > Thanks--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---