Thanks, T.J. : )
I'm still in doubt, though, whether "elegant" code is better than
straightforward one.
If it wasn't straight-obvious for you what the snippet does, there's a
big chance it won't be obvious for another developer looking at it (or
to myself in half a year) :)
- kangax
On Apr 28, 9:36 am, "T.J. Crowder" <[EMAIL PROTECTED]> wrote:
> @kangax -
>
> > elements.shift().observe('click', function(e) {
> > elements.invoke('writeAttribute', 'disabled', e.target.checked);
>
> That is just so...elegant. Took me a while to figure it out (missed
> that part of the OP's question) and *wow* would I comment it, but
> seriously: Nice.
> --
> T.J. Crowder
> tj / crowder software / com
>
> On Apr 28, 12:23 am, kangax <[EMAIL PROTECTED]> wrote:
>
> > I'll try to be careful this time.
>
> > 1) Straight-forward way, only checks whether id starts with a string:
> > var elements = $$('input[type="checkbox"][id^="match_this_"]');
>
> > 2) A little more precise (based on regexp):
> > var elements = $$('input[type="checkbox"]').findAll(function(el)
> > { return /^match_this_\d$/.test(el.id) });
>
> > Then:
>
> > elements.shift().observe('click', function(e) {
> > elements.invoke('writeAttribute', 'disabled', e.target.checked);
>
> > })
>
> > - kangax
>
> > On Apr 27, 6:40 pm, zoopzoop <[EMAIL PROTECTED]> wrote:
>
> > > Hi!
>
> > > I want to (as the topic suggests) get a list/array of all elements on
> > > a page whose id starts with a string using Prototype.
> > > For example, I have several checkboxes like this:
>
> > > <input type="checkbox" id="match_this_1">
> > > <input type="checkbox" id="match_this_2">
> > > <input type="checkbox" id="match_this_3">
> > > <input type="checkbox" id="match_this_4">
>
> > > I want to find these elements on my page based on the fact that their
> > > ids start with "match_this".
> > > Then I want to assign a onclick-function to the first checkbox that
> > > disables all the other ones.
> > > Can somebody help me out with how to do this with Prototype?
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---