Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Combining object arrays

20 views
Skip to first unread message

mike

unread,
Sep 14, 2005, 4:37:25 PM9/14/05
to
If I have 2 object arrays like:

var txtobj = theform.getElementsByTagName("input");
var selobj = theform.getElementsByTagName("select");

and i want to iterate over them I'd like to combine them and then
iterate over them.

so I would do something like this below, but that doesn't look right.
var bothobj = txtobj+selobj;

for( var i=0; i<bothobj.length; i++ )
{
do something....
}

Any thought?

Randy Webb

unread,
Sep 14, 2005, 5:53:52 PM9/14/05
to
mike said the following on 9/14/2005 4:37 PM:

Test it and see.

.concat()

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly

Mick White

unread,
Sep 14, 2005, 6:10:02 PM9/14/05
to
mike wrote:

> If I have 2 object arrays like:
>
> var txtobj = theform.getElementsByTagName("input");
> var selobj = theform.getElementsByTagName("select");

Bad!!
var txtobj = document.theform.getElementsByTagName("input")

But why not use DOM 0?

function checkInputsAndSelects(form){
var f=form.length;
while(f--){
if(form[f].type.toLowerCase()=="input"){
// do stuff with input
}
if(form[f].type.toLowerCase()=="select"){
// do stuff with select
}
}
}

Mick

[...]

mike

unread,
Sep 14, 2005, 6:22:11 PM9/14/05
to
Mick,

Why is this bad?

var txtobj = document.theform.getElementsByTagName("input")

Is it because you think it only gets <input ..> but not <INPUT ...>

Mike

Mick White

unread,
Sep 14, 2005, 6:53:10 PM9/14/05
to
mike wrote:

var txtobj = theform.getElementsByTagName("input");

missing "document" reference...

Will work in IE, though.

Mick

mike

unread,
Sep 14, 2005, 7:20:45 PM9/14/05
to
i had already defined

var theform = document.update;

so ... it is ok then ... ?

RobG

unread,
Sep 14, 2005, 8:12:40 PM9/14/05
to
mike wrote:
> If I have 2 object arrays like:
>
> var txtobj = theform.getElementsByTagName("input");
> var selobj = theform.getElementsByTagName("select");
>
> and i want to iterate over them I'd like to combine them and then
> iterate over them.
>
> so I would do something like this below, but that doesn't look right.
> var bothobj = txtobj+selobj;

No, it's not right. getElementsByTagName returns an HTML collection,
not an array. Collections have some array-like properties, e.g. length,
but have none of an Array's methods.

>
> for( var i=0; i<bothobj.length; i++ )
> {
> do something....
> }

To concatenate collections, you could create a concatenation function
that adds the elements of a collection to an array[1]:

function concatCollections() {
var c, k, j, i = arguments.length;
var a = [];
for ( j=0; j<i; j++ ) {
c = arguments[j];
h = c.length;
for ( k=0; k<h; k++ ){
a.push(c[k]);
}
}
return a;
}

And once you've created the collections, call the above function:

var arrayOfElements = concatCollections(txtobj, selobj);

But this seems a waste of time. Whatever function that is going to
iterate over the array could accept multiple arguments and iterate over
collections instead.

Unless there are some array methods you'd like to use.

[1] Push is not supported in very old browsers (it was introduced in
JavaScript 1.2, works in Netscape 3+ and IE 5+ I think), it's pretty
simple to create your own push function if required.
<URL:http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Global_Objects:Array:push>


--
Rob

Mick White

unread,
Sep 14, 2005, 8:38:22 PM9/14/05
to
mike wrote:

Your original post:

<QUOTE>


If I have 2 object arrays like:

var txtobj = theform.getElementsByTagName("input");


var selobj = theform.getElementsByTagName("select");

[...]
</QUOTE>

Missing "document" reference.
Mick

mike

unread,
Sep 15, 2005, 10:12:26 AM9/15/05
to
But this seems a waste of time. Whatever function that is going to
iterate over the array could accept multiple arguments and iterate over

collections instead.

I agree. thanks though for your explanation and collection code.

Mike

Message has been deleted

mike

unread,
Sep 18, 2005, 10:20:42 AM9/18/05
to
This is what I decided to do:

var theform = document.update;

var txtobj = theform.getElementsByTagName("input");

var mod = do_something(txtobj);
... something happens with the results of mod

var selobj = theform.getElementsByTagName("select");

var mod = do_something(selobj);

... something happens with the results of mod

function do_something(obj)
{
for( var i=0; i<obj.length; i++ )
{
do something....
return .....
}
}

Mike

0 new messages