By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,466 Members | 1,148 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,466 IT Pros & Developers. It's quick & easy.

make array of page elements

P: n/a
Hello,

I need to make an array of elements accross forms.
My javascript skills, as evident from this question,
are rather rudimentary.

I tried to make an associative array and index it
with the object references. However, I just realized
that indices may only be referenced by strings.

Maybe someone can suggest a better way to identify
individual page elements, or convert object reference
to some unique string?

One way I thought of accomplishing this would be to
concatenate a form name + element name + element id
and reference with that. This last way somehow seems
clumsy, but maybe this is the only way? Any suggestions?

thanks!
Jul 23 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Denis Perelyubskiy wrote:
Hello,

I need to make an array of elements accross forms.
My javascript skills, as evident from this question,
are rather rudimentary.

var myArray = new Array()
var counter = 0;
function doIt(){
var total=document.forms.length;

for (i=0;i<total;i++){

for (j=0;j<document.forms[i].length;j++){
myArray[counter] =
[document.forms[i].name,document.forms[i].elements[j].name]
counter++
}
}
}
I tried to make an associative array and index it
with the object references. However, I just realized
that indices may only be referenced by strings.
huh?
Maybe someone can suggest a better way to identify
individual page elements, or convert object reference
to some unique string?


Don't use a single string, use an array of arrays where the array entry
holds the name of the form and the name of the input. You can then
access the parent array via Index Number.
--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq
Jul 23 '05 #2

P: n/a
Randy Webb wrote:
Denis Perelyubskiy wrote:
I tried to make an associative array and index it
with the object references. However, I just realized
that indices may only be referenced by strings.

huh?


Well, maybe I am confused. I tried to just
iterate through forms/elements with
formElement[j] being the current element,
and simply add them to my array like so:

array[formElement[j]];

But then I read that array index may only
be an integer or a string - and maybe I
misunderstood this part. Please correct
me if I was wrong.

http://member.melbpc.org.au/~tgosbel...vascript-hash/

When trying formElement[j] as an index,
I was hoping that maybe I can use
references (and I judged formElement[j] to be
a reference) much as pointers in C, which may serve
as unique identifiers in some circumstances.
Clearly I was wrong.

I did not fully understand an idea about
double-dimentional arrays. Specifically,
I did not understand the advantage of
this multi-dimentional approach, as
opposed to simply concatenating various
pieces of information.

thanks.
Jul 23 '05 #3

P: n/a
Denis Perelyubskiy wrote:
Randy Webb wrote:
Denis Perelyubskiy wrote:
I tried to make an associative array and index it
with the object references. However, I just realized
that indices may only be referenced by strings.
huh?

Well, maybe I am confused. I tried to just
iterate through forms/elements with
formElement[j] being the current element,
and simply add them to my array like so:

array[formElement[j]];

But then I read that array index may only
be an integer or a string - and maybe I
misunderstood this part. Please correct
me if I was wrong.

http://member.melbpc.org.au/~tgosbel...vascript-hash/


I think you misunderstood it.
<quote>
But in JavaScript, the index of an array need not be a number, it can
also be a word or key as listing 1 demonstrates.
</quote>

"need not be..." means it can be but doesn't have to be.

When trying formElement[j] as an index,
I was hoping that maybe I can use
references (and I judged formElement[j] to be
a reference) much as pointers in C, which may serve
as unique identifiers in some circumstances.
Clearly I was wrong.

I did not fully understand an idea about
double-dimentional arrays. Specifically,
I did not understand the advantage of
this multi-dimentional approach, as
opposed to simply concatenating various
pieces of information.


If you concatenate the information, when you go back to access that
element again, you have to split it apart to get to the element, so why
even join it to begin with?

But what are you trying to accomplish?
--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq
Jul 23 '05 #4

P: n/a
On Sun, 09 Jan 2005 02:03:21 -0500, Randy Webb <Hi************@aol.com>
wrote:

[snip]
<quote>
But in JavaScript, the index of an array need not be a number, it can
also be a word or key as listing 1 demonstrates.
</quote>


You can use any expression within square brackets, but the expression will
*always* be coerced into a string. As all FORM elements have a toString
method which returns the same value, they aren't suitable keys in a plain
"hash table".

[snip]

Mike

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.
Jul 23 '05 #5

P: n/a
Randy Webb wrote:
Denis Perelyubskiy wrote:

<snip>
... . Please correct
me if I was wrong.

http://member.melbpc.org.au/~tgosbel...vascript-hash/


I think you misunderstood it.
<quote>
But in JavaScript, the index of an array need not be a number,
it can also be a word or key as listing 1 demonstrates.
</quote>

<snip>

When an article is headed "Real programming with JavaScript -
Associative Arrays" and then goes on to demonstrate a failure to
comprehend the javascript specification (ECMA 262) then maybe it is not
such a good place to go looking for advice/information on the subject.

First, there is no specific "array indexing" syntax in javascript. There
are only property accessors; dot notation property accessors and bracket
notation property accessors. Property accessors are used to reference
the named properties of objects (for value assignment, value reading and
deletion). However, because a dot notation property accessor may only
use legal identifiers, and a character sequence that resembles a number
cannot be a legal identifier, the referencing of the properties of an
array that represent the array elements can only be done with bracket
notation property accessors (where any character sequence can be used in
a property name).

When a bracket nation property accessor references a property of any
object the expression used within the brackets is type-converted in to a
string (as required by the property accessor algorithm: ECMA 262 3rd
edition Section 11.2.1). The nature and mechanism of this
type-conversion depends on the type of the value resulting form the
evaluation of the expression, as specified for the internal ToString
function (ECMA 262 3rd edition Section 9.8).

Thus, given the array - var a = ['x',.'y','.z']; -, the property
accessors - a[1] - and - a['1'] - are identical in their specified
behaviour.

A javascript array is no more than an ordinary javascript object (the
'native ECMAScript Object' that has undergone some internal augmentation
when it was created. Specifically, it has had its internal [[Prototype]]
property assigned a reference to - Array.prototype - (so that the Array
methods can be inherited), its internal [[Class]] property has been
assigned the string "Array", a '- length - property is created and
initialised (see ECMA 262 3rd edition Section 15.4.2.1-2 for
initialisation details) and (above all) its internal [[Put]] method is
replaced with the one that gives an Array object all of its special
characteristics. (note though that the internal [[Get]] method is
completely unchanged from the normal Object [[Get]] method).

The internal [[Put]] method takes a property name (string) and a value
as its arguments, and assigns the value provided to a property of the
object with the given name. The special [[Put]] method employed by Array
objects performs some additional steps when it is used to assign a value
to its object. The Array's [[Put]] method must be interested in the
assignments to the - length - property of an array, because if it is
less than the current value it may be necessary to delete properties
from the array. Otherwise, the property name string argument is
converted into a number using the internal ToUnit32 function, and if
that number is not less than the current value of the - length -
property then the - length - property of the Array is re-set to the
value of that number plus one.

Whenever the property name provided to the Array's [[Put]] method is not
a string representation of an (unsigned 32 bit) integer number clause 8
in the algorithm for the [[Put]] method avoids the need to consider
interaction with the Array's - length - property. Clause 8 is somewhat
vague in saying "If P is not an array index, return". But the
specification states that a property name string is an array index when
ToString(ToUnit32(propertyName)) equals propertyName, where 'equals'
means; exactly the same characters in exactly the same sequence (ECMA
262 3rd edition Section 15.4).

(It is interesting to note that the augmented object used as an Array
does not have a special [[Delete]] method assigned, so deleting an array
element has no specified effect on the Array's - length property.)

However, none of the above has anything to do with implementing
associative array or hashtable like behaviour in javascript as the
characteristics that distinguish an Array from any other native
ECMAScript Object relate to the handling of property names that satisfy
the definition of an "array index" and the array's length property.
Associative arrays and hashtable implementations employ a key-value pair
instead of array index. These implementations are taking advantage of
characteristics that are common to all native ECMAScirpt objects; the
ability to add named properties to any Object.

As a result, any discussion of implementing associative array or
hashtable like behaviour in javascript that commences with:-

var a = new Array();

- (or its equivalent) is written with a fundamental misconception (and
likely to promote confusion and misconceptions in its readers). In
reality, when name-value pairs are the only facility required of an
object then using an Array for that object will result in consequential
loss of performance because the Array's [[Put]] method has a more
involved algorithm and so will be slower in operation.

Richard.
Jul 23 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.