473,395 Members | 1,823 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,395 software developers and data experts.

make array of page elements

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
5 6486
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: ajay | last post by:
I have following code for a slide menu but i twiked it to work for a single level menu. Open it in a Browser to get a clear picture. I have 2 Qs 1) How to make first entry as non-link. i.e i...
1
by: Michael Hill | last post by:
I am creating a page where I want to dynamically fillin the contents of a table based on user selections. The page is created from a cgi. I have a number of rows, say 150 that I want to hide...
35
by: VK | last post by:
Whatever you wanted to know about it but always were affraid to ask. <http://www.geocities.com/schools_ring/ArrayAndHash.html>
10
by: Bob | last post by:
Here's what I have: void miniVector<T>::insertOrder(miniVector<T>& v,const T& item) { int i, j; T target; vSize += 1; T newVector; newVector=new T;
3
by: Newcomsas | last post by:
Hello, I'm trying to solve a problem with JS textbox array without success. I have two buttons in my page: PLUS and MINUS; at every click on PLUS a new textbox named 'dear' is generated. So, if...
6
by: scottyman | last post by:
I can't make this script work properly. I've gone as far as I can with it and the rest is out of my ability. I can do some html editing but I'm lost in the Java world. The script at the bottom of...
14
by: dan | last post by:
I would like to have the preprocessor automatically generate the number of array elements requested. Each element is zero. The elements get pasted into a larger array. The other elements may be...
7
by: arnuld | last post by:
this programme gives unusual output. i am not able to find out where the semantic bug lies: /* C++ Primer - 4/e * * an example from section section 7.2.4, page 241 * STATEMENT * write a...
15
by: Michael B Allen | last post by:
I like to use limit pointers as sentinels when working on buffers of data. Meaning I use ptr < lim where ptr and lim are both pointers rather than n 0 to determine if it is safe to proceed writing...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.