View Full Version : Quick Q: Grabbing Elements from Array

07-04-2005, 03:05 AM

I am having trouble with this:


As you can see, the name of the form field I want to grab depends on a previous variable: "no"

and therefore the name of the form element is:


How can I do this using JS?

Thank you for your help!!

07-04-2005, 03:15 AM
I am having trouble with this:

document.formname."Course_"+no.value;One can construct property names at run-time using square bracket notation:

document.forms.formname.elements['Course_' + no.value]The expression between the brackets will be evaluated (a string concatenation in this case), then converted to a string. It is this string that's used as the property name.

I'd like to point out that this has nothing to do with arrays. It's a general language feature and works with any object.

Hope that helps,

07-04-2005, 03:25 AM

That's a big help.

Thank you very much.

One more quick thing, if you have a sec.

In PHP, I can define an array as follows: $array = ('value1'=>'1', 'value2'=>'2');

This is a much easier way to specify custom indexes for my array.

Is there a similar way to do this in JS?


07-04-2005, 03:50 PM
In PHP, I can define an array as follows: $array = ('value1'=>'1', 'value2'=>'2');I though that might come up, which is why I made the note about not being array specific.

First, you need to consider if this is really an array. In PHP, arrays are associative in that indices can be both numbers and strings, and the array functions apply equally either way. This is not true of Javascript (or more formally, ECMAScript) as there is no concept of associative arrays. This is a source of some confusion, particularly as some sites try to convince readers of the contrary.

Arrays, and Array methods, work only with numeric indicies. The indices can be strings that represent numbers (like '1' and '54', but not '01' or '0x32'), but they are all still essentially numbers, nonetheless. For example,

var a = new Array();

a[0] = 'some value';creates an array and assigns it one element. The length property of that array will be one (1), and all Array methods will operate based on that one element only.

a[2] = 'some other value';adds another element, but as arrays are sparse (you can skip elements), this will actually increase the length property to three, and Array methods will behave as if there were three elements. The value of a[1] will be the primitive type, undefined.

Hopefully, so far so good. However,

a['key'] = 'yet another value';tends to throw a spanner in the works. Some people expect that this will be treated as another array element, but it won't. The length property will be unaltered, and none of the Array methods will consider this property when they are called.

As I said in my previous post, this behaviour is a language feature and any object - Array, Boolean, Math, Number, Object, RegExp, String, and even host objects (like HTML elements) - can exhibit the effects.

So, back to the question I asked above: do you actually want an array(-like) object, or just some container that you can shove values into and access them by name? To do the former properly, you'll need to produce a fair amount of code to result in a decent hashtable implementation (I have one, but I should probably revise it thoroughly and remove some of its deficiencies). If you just want a simple container, you can use an object literal:

/* The literal characters are braces. */
var container = {key1 : 'value1',
key2 : 'value2'};If you want to use characters that can't appear in identifiers (like spaces, or property names begining with numbers), the surround the property name in quotes ('some key', for example).

Hope that helps,

07-04-2005, 11:38 PM

Thanks for that, makes perfect sense.

- James