Hi,

> obviously that I'll never have really an array of controls... but if I
> get an array when I use getElementsByName()

If you have the brackets in the names, getElementsByName isn't going
to get you the elements either:

HTML:
<input type='text' name='test[1]' value='text 1'>
<input type='text' name='test[2]' value='text 2'>
<input type='text' name='test[3]' value='text 3'>
<input type='text' name='test[4]' value='text 4'>
<input type='text' name='ignoreme[1]' value='ignore this one'>
<input type='text' name='ignoreme[2]' value='also ignore this one'>

JavaScript:
var list = document.getElementsByName('test');
alert(list.length);

Result:
Alerts "0"

If they all had the exact same name, it would, but not if they also
have brackets with differing numbers inside.

> my question was whether to "prototype" implements a short call to do
> the same... something like $X('myarray')

Prototype provides several features that help in this situation.  The
two most useful, probably, have already been mentioned -- Douglas
pointed out that using classes and Prototype's $$ function could
easily solve the problem, and Colin pointed out that Prototype
provides nearly all of CSS Level 3 selectors, even on browsers that
don't have them natively.

Something else that might help is Prototype's extensive features for
arrays and enumerables, like Enumerable#findAll[1], which lets you
easily filter an enumerable for the items you want.  Combined with
Element#select[2] (if all of the elements you want have a common
parent) or $$[3] (if you want to look globally in the document), it's
very easy to filter:

HTML:
<input type='text' name='test[1]' value='text 1'>
<input type='text' name='test[2]' value='text 2'>
<input type='text' name='test[3]' value='text 3'>
<input type='text' name='test[4]' value='text 4'>
<input type='text' name='ignoreme[1]' value='ignore this one'>
<input type='text' name='ignoreme[2]' value='also ignore this one'>

JavaScript:
var list = $$('input[type=text]').findAll(function(elm) {
    return (elm && elm.name && elm.name.startsWith('test'));
});
alert(list.length);

Result:
Alerts "4"

In there, I also used another feature of Prototype, String#startsWith
[4].

But if you can use classes, or there's some way to identify these
things with just CSS selectors, all the better.

[1] http://prototypejs.org/api/enumerable/findall
[2] http://prototypejs.org/api/element/select
[3] http://prototypejs.org/api/utility/dollar-dollar
[4] http://prototypejs.org/api/string/startswith

HTH,
--
T.J. Crowder
tj / crowder software / com
Independent Software Engineer, consulting services available


On Jul 25, 10:36 pm, diegoturriaga <diegoturri...@gmail.com> wrote:
> maybe I haven't explained well (my English is not good) or I'm reading
> or translating wrong the answers
>
> obviously that I'll never have really an array of controls... but if I
> get an array when I use getElementsByName()
>
> my question was whether to "prototype" implements a short call to do
> the same... something like $X('myarray')
>
> Well, what I was doing and I finished, so we can close this topic.
>
> On 24 jul, 07:11, ColinFine <colin.f...@pace.com> wrote:
>
>
>
> > On Jul 24, 1:02 am, diegoturriaga <diegoturri...@gmail.com> wrote:
>
> > > Well... first that all thanks for your reply!
>
> > > I'm trying to use a multidemensional javascript array... various
> > > arrays of input controls of type checkbox
>
> > > I have 10 controls with name "myarray[1][]"
> > > other 10 controls with name "myarray[2][]"
> > > other 10 controls with name "myarray[3][]"
> > > ...
> > > other 10 controls with name "myarray[n][]"
>
> > > And I need get al controls named iqual in an array var... something
> > > like:
>
> > > var aux = document.getElemntsByName('myarray[1][]`);
>
> > > but using prototype sintax... :)
>
> > I'm not sure if you are under a misapprehension.
>
> > The value of a 'NAME' property in HTML is a CDATA - a string of text.
> > It has no structure.
> > You are free to call an input "opt[0][]" but that is a text string,
> > not any kind of an array in any language.
> > (PHP kindly treats inputs with [] in their names specially, but that
> > is the server-side program, not anything to do with the page or
> > javascript: I don't know whether any other server-side languages do
> > this).
>
> > So if you want to use prototype (or any other javascript) to
> > manipulate these names, you must treat them as text: match them as
> > text, perhaps even 'eval' them. But they are not arrays in any way.
>
> > As Matt says, the nearest you can get to this in Prototype is by using
> > the =~ match in a CSS selector, but that will not necessarily do what
> > you need. Otherwise you will have to do basic Javascript text
> > matching, or use the suggestions others have made.
>
> > (It may be that you already understand the point I am making, in which
> > case, my apologies).- Ocultar texto de la cita -
>
> > - Mostrar texto de la cita -
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Prototype & script.aculo.us" group.
To post to this group, send email to prototype-scriptaculous@googlegroups.com
To unsubscribe from this group, send email to 
prototype-scriptaculous+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/prototype-scriptaculous?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to