$$ returns an Array, not any Enumerable :) Best, -foca
On Nov 14, 2007 6:03 PM, Gareth Evans <[EMAIL PROTECTED]> wrote: > Oh, didn't realise an enumerable had the array methods available. > You can't use the [ ] notation so I assumed it wouldnt work and didnt > actually try it. > > In your example, yeah i'd agree you'd have to use some sort of indexing > scenario. > Though, using index like the second example works? The 'index' is only > available while iterating, at the time the event fires, wouldn't it be gone, > closure or no closure? > > I spoke to Mathieu who has made a change to the datepicker to allow it to > be initialized if it hasnt when you click it, which solves my 2) question, > but still wondering why that would be a bad idea anyway. > > > > On 11/15/07, kangax <[EMAIL PROTECTED]> wrote: > > > > > > Gareth, > > > > http://www.prototypejs.org/api/array/first > > and > > http://www.prototypejs.org/api/array/last > > should do the trick. > > > > The problem arises when you need to access next element in a > > collection. Consider this example: > > > > $$('input[type=text]').invoke('observe', 'keypress', function(e) { > > if (Event.KEY_TAB == e.keyCode) { > > // focus next element in collection > > } > > }) > > > > Inside a callback we could resort to this.next('input[type=text]'), > > but this unfortunately will not work if input elements are not in a > > linear order > > One way to make it happen is by storing index of a current element in > > a closure and focusing the element with index = index + 1 when the key > > is pressed > > > > $$('input[type=text]').each(function(el, index) { > > el.observe('keypress', function(e)) { > > if (Event.KEY_TAB == e.keyCode) { > > $$('input[type=text]')[index+1].focus() > > } > > } > > }) > > > > > > > > > > > > > > --~--~---------~--~----~------------~-------~--~----~ 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 [email protected] To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
