473,320 Members | 1,921 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Combining object arrays

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?

Sep 14 '05 #1
10 7024
mike said the following on 9/14/2005 4:37 PM:
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?


Test it and see.

..concat()

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Sep 14 '05 #2
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

[...]
Sep 14 '05 #3
Mick,

Why is this bad?

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

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

Mike

Sep 14 '05 #4
mike wrote:
Mick,

Why is this bad?

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

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


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

missing "document" reference...

Will work in IE, though.

Mick
Sep 14 '05 #5
i had already defined

var theform = document.update;

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

Sep 14 '05 #6
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
Sep 15 '05 #7
mike wrote:
i had already defined

var theform = document.update;

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

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
Sep 15 '05 #8
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

Sep 15 '05 #9
Lee
mike said:

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.


You're probably handling the input's and the select's differently
anyway, so why bother with pulling them out and concatinating them?

for (i=0;i<theform.elements.length;i++) {
if (theform.elements[i].type=="input") {
} else if (...) {
...
}
}

Sep 15 '05 #10
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

Sep 18 '05 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Phil Powell | last post by:
if (is_array($_POST)) { foreach ($this->getAssocSectionsObjArray($key, $dbAP) as $obj) { print_r($obj); print_r(" in array? "); print_r(in_array($obj, $result)); print_r("<P>"); if...
2
by: Chris Mullins | last post by:
I've spent a bit of time over the last year trying to implement RFC 3454 (Preparation of Internationalized Strings, aka 'StringPrep'). This RFC is also a dependency for RFC 3491...
38
by: VK | last post by:
Hello, In my object I have getDirectory() method which returns 2-dimentional array (or an imitation of 2-dimentional array using two JavaScript objects with auto-handled length property - please...
3
by: Flip | last post by:
I'm looking at the O'Reilly Programming C# book and I have a question about extending and combining interfaces syntax. It just looks a bit odd to me, the two syntaxes look identical, but how does...
1
by: Mr. Jingles | last post by:
Hi, anyone have any thoughts on combining two 2-Dimensional arrays into one. Thanx
1
by: ferraro.joseph | last post by:
Hi, I'm querying Salesforce.com via their AJAX toolkit and outputting query results into a table. Currently, their toolkit does not possess the ability to do table joins via their structured...
1
by: Jeff | last post by:
I have two array: var Array1=new Array(); Array1=,,]; Array1=,,]; var Array2=new Array(); Array2=,,];
6
by: tshad | last post by:
I am looking for a way to combine 2 string arrays. I am trying to get a list of files from 2 directories and combine them: string strFiles; string strFiles2; strFiles =...
8
by: rodeored | last post by:
I don't know what the official programming lingo is for this situation but there probably is one. I have arrays, which happen to be parsed urls, and I want to make one big array with each subarray...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.