Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > javascript values to array

Reply
Thread Tools

javascript values to array

 
 
rodchar
Guest
Posts: n/a
 
      04-09-2008
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
 
Reply With Quote
 
 
 
 
bruce barker
Guest
Posts: n/a
 
      04-09-2008
use array initializer:

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

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

var mycars= {};
var boxes = document.getElementById('cars').getElementsByTagNa me('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

 
Reply With Quote
 
 
 
 
rodchar
Guest
Posts: n/a
 
      04-09-2008
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 <div id="cars">
>
> var mycars= {};
> var boxes = document.getElementById('cars').getElementsByTagNa me('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

 
Reply With Quote
 
bruce barker
Guest
Posts: n/a
 
      04-10-2008
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[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 <div id="cars">
>>
>> var mycars= {};
>> var boxes = document.getElementById('cars').getElementsByTagNa me('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

 
Reply With Quote
 
Anthony Jones
Guest
Posts: n/a
 
      04-10-2008
"bruce barker" <(E-Mail Removed)> wrote in message
news:%(E-Mail Removed)...
> 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[i]);
>




--
Anthony Jones - MVP ASP/ASP.NET


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
const and array of array (of array ...) Mara Guida C Programming 3 09-03-2009 07:54 AM
Array of attribute values from object array? danielbigg@googlemail.com Javascript 11 04-13-2009 11:24 AM
JavaScript Array Values Laurent Bugnion ASP .Net 4 10-24-2006 02:25 AM
Repost: Validation with JavaScript and SQL Server db vs. Array list values Kermit Piper Javascript 4 02-13-2006 06:47 AM
Length of Array of Array of Array Tom Perl Misc 3 12-20-2004 05:23 PM



Advertisments