javascript values to array

  • Thread starter Thread starter rodchar
  • Start date Start date
R

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
 
use array initializer:

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

put the textboxes in a <div id="cars">

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

-- bruce (sqlwork.com)
 
what is this line doing?
mycars[mycars.length] = boxes.value;



bruce barker said:
use array initializer:

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

put the textboxes in a <div id="cars">

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

-- bruce (sqlwork.com)


rodchar said:
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
 
mycars is an empty array, boxes is an array of <input> 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);

-- bruce (sqlwork.com)

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



bruce barker said:
use array initializer:

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

put the textboxes in a <div id="cars">

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

-- bruce (sqlwork.com)


rodchar said:
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
 
bruce barker said:
mycars is an empty array, boxes is an array of <input> 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);
 
Back
Top