javascript values to array

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
 
B

bruce barker

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)
 
R

rodchar

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
 
B

bruce barker

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
 
A

Anthony Jones

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);
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top