473,714 Members | 2,552 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 6523
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.javas cript 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.javas cript 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(ToUnit 32(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
4327
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 want to make first text as Table Heading/menu category. For examle in the given menu i want to make a heading as "Comp. Languages" which won't be a link. 2) The position of this menu is absolute to the page. I want to make it absolute to the Table...
1
1569
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 in the page, either with a xml or in an array as per below. I don't which one I'd like to use
35
6650
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
4836
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
6934
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 one clicks, say, 3 times the output is something like: dear dear dear
6
4889
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 the html page controls the form fields that are required. It doesn't function like it's supposed to and I can leave all the fields blank and it still submits the form. Also I can't get it to transfer the file in the upload section. The file name...
14
12441
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 non-zero. ***** Here is an example of what I need to do: #define YEAR_1 2005 #define YEAR_2 2007 #define YEARS (YEAR_2 - YEAR_1 + 1)
7
3411
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 function that prints the elements of an array. don't use pointer to an array as parameter because pointer will be copied, use reference to the array instead.
15
2906
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 to that location. The rationale is that it is simpler and thus safer because you're not constantly recomputing integer n. However, I've run into a case where this fails and I'd like to know what the experts would do. If I want to precompute...
0
9314
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9174
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9015
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7953
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6634
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5947
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4464
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3158
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2520
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.