javascript values to array

javascript values to array

am 09.04.2008 21:05:03 von rodchar

hey all,
is there an easy way to take the values of 8 textboxes and put it in an
javascript array? i know there's a straightforward way to do it i was just
wondering if there are some other interesting ways?

Straightforward way:
var mycars = new Array();
mycars[0] = "car1";
mycars[1] = "car2";
mycars[2] = "car3";
....

thanks,
rodchar

RE: javascript values to array

am 09.04.2008 21:38:06 von brucebarker

use array initializer:

var mycars = {'car1','car2','car3'};

put the textboxes in a



var mycars= {};
var boxes = document.getElementById('cars').getElementsByTagName('input' );
for (var i=0; i < boxes.length; ++i)
mycars[mycars.length] = boxes[i].value;

-- bruce (sqlwork.com)


"rodchar" wrote:

> hey all,
> is there an easy way to take the values of 8 textboxes and put it in an
> javascript array? i know there's a straightforward way to do it i was just
> wondering if there are some other interesting ways?
>
> Straightforward way:
> var mycars = new Array();
> mycars[0] = "car1";
> mycars[1] = "car2";
> mycars[2] = "car3";
> ...
>
> thanks,
> rodchar

RE: javascript values to array

am 09.04.2008 22:55:00 von rodchar

what is this line doing?
> mycars[mycars.length] = boxes[i].value;


"bruce barker" wrote:

> use array initializer:
>
> var mycars = {'car1','car2','car3'};
>
> put the textboxes in a


>
> var mycars= {};
> var boxes = document.getElementById('cars').getElementsByTagName('input' );
> for (var i=0; i < boxes.length; ++i)
> mycars[mycars.length] = boxes[i].value;
>
> -- bruce (sqlwork.com)
>
>
> "rodchar" wrote:
>
> > hey all,
> > is there an easy way to take the values of 8 textboxes and put it in an
> > javascript array? i know there's a straightforward way to do it i was just
> > wondering if there are some other interesting ways?
> >
> > Straightforward way:
> > var mycars = new Array();
> > mycars[0] = "car1";
> > mycars[1] = "car2";
> > mycars[2] = "car3";
> > ...
> >
> > thanks,
> > rodchar

Re: javascript values to array

am 10.04.2008 02:52:07 von DFS

mycars is an empty array, boxes is an array of objects. for each
boxes, its value is added to the array mycars.

in javascript all arrays are associative arrays, so elements can be
added at any index:

var myArray = {}; // new empty array
myArray[1] = "one";
myArray[5] = "five";

myArray has two elements, indexed by 1 or 5, myArray[0] returns
undefined, as does myArray.length (as the array is sparse). to iterate
over a sparse array use the for(in):

for(var i in myArray)
alert("idx: ' + i + " value: " + myArray[i]);

-- bruce (sqlwork.com)


rodchar wrote:
> what is this line doing?
>> mycars[mycars.length] = boxes[i].value;
>
>
> "bruce barker" wrote:
>
>> use array initializer:
>>
>> var mycars = {'car1','car2','car3'};
>>
>> put the textboxes in a


>>
>> var mycars= {};
>> var boxes = document.getElementById('cars').getElementsByTagName('input' );
>> for (var i=0; i < boxes.length; ++i)
>> mycars[mycars.length] = boxes[i].value;
>>
>> -- bruce (sqlwork.com)
>>
>>
>> "rodchar" wrote:
>>
>>> hey all,
>>> is there an easy way to take the values of 8 textboxes and put it in an
>>> javascript array? i know there's a straightforward way to do it i was just
>>> wondering if there are some other interesting ways?
>>>
>>> Straightforward way:
>>> var mycars = new Array();
>>> mycars[0] = "car1";
>>> mycars[1] = "car2";
>>> mycars[2] = "car3";
>>> ...
>>>
>>> thanks,
>>> rodchar

Re: javascript values to array

am 10.04.2008 10:35:04 von Anthony Jones

"bruce barker" wrote in message
news:%23xa5bUqmIHA.5820@TK2MSFTNGP04.phx.gbl...
> mycars is an empty array, boxes is an array of objects. for each
> boxes, its value is added to the array mycars.
>
> in javascript all arrays are associative arrays,

This isn't strictly true. The javascript arrays aren't assocative at all.

var myArray = ["one", "five"]

this array has .length = 2

>so elements can be
> added at any index:
>
> var myArray = {}; // new empty array

The above isn't really an array its an object.

> myArray[1] = "one";
> myArray[5] = "five";

the object now has two attributes attached "1" and "5" note attributes can
only be strings the values of which are "one" and "five". The ability to
create attributes on an object in this way allows an object to be used as an
associative array (with the limitation that it doesn't support a length
property).

>
> myArray has two elements, indexed by 1 or 5, myArray[0] returns
> undefined, as does myArray.length (as the array is sparse). to iterate
> over a sparse array use the for(in):
>
> for(var i in myArray)
> alert("idx: ' + i + " value: " + myArray[i]);
>



--
Anthony Jones - MVP ASP/ASP.NET