actually, i think the most efficient way to do this is to mark the questions
and answers with grouped id's:
<div id='q14' class = 'question'></div>
<div id='a14' class = 'answer'></div>
and then you could do:
a_id = 'a'+this.get('id').substr(1);
this is much simpler and more efficient than creating an array of all
answers and question, and IMO more semantic as well.
if you anticipate a question will be clicked multiple times than element
storage can be a good solution, but in a q-a scenario i think most times
element storage is an overkill.
-----------
אריה גלזר
052-5348-561
5561
On Wed, Feb 10, 2010 at 23:48, cbolson <[email protected]> wrote:
> Thanks all for your help :)
> I think that it was the indexOf that I was missing in all my various
> attempts.
>
> Using getNext(), whilst it works in my example in mooshell, won't
> actually work in my actual code as the elements aren't "together".
>
> Cheers,
> Chris
>
> On 10 feb, 22:30, Ryan Florence <[email protected]> wrote:
> > I'd personally create the arrays and do the indexOf in this scenario.
> >
> > But there's a chance the actual use case could call for storing the
> > answers with the questions then simply retrieving the element using
> > el.store and el.retrieve.
> >
> > On Feb 10, 2010, at 2:04 PM, אריה גלזר wrote:
> >
> > >http://mootools.net/shell/K55zK/3/
> > > this in an event always points to the element.
> > > knowing that, here are 2 solutions for this that i can think of:
> > > 1. creating a questions array and using indexOf
> > > 2. using getNext() to fetch the nearest answer
> >
> > > both are shown in the example above.
> > > -----------
> > > אריה גלזר
> > > 052-5348-561
> > > 5561
> >
> > > On Wed, Feb 10, 2010 at 22:41, cbolson <[email protected]> wrote:
> > > Is it possible to get the index of the element (eg) clicked when using
> > > event delegation?
> >
> > > Here is my example code:
> >
> > > var comments=$$('.answer').hide();
> >
> > > document.id('faq').addEvent('click:relay(.question)', function(){
> > > // how to get the "index" of the item clicked so that this
> > > next line will work?
> > > comments[index].show();
> > > });
> >
> > > Code in mooshell:
> > >http://mootools.net/shell/kss2C/
> >
> > > Thanks in advance for any suggestions, I am probably missing something
> > > obvious :(
> >
> > > Chris
>