javascript values to array

Discussion in 'ASP .Net' started by rodchar, Apr 9, 2008.

  1. rodchar

    rodchar Guest

    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
    rodchar, Apr 9, 2008
    #1
    1. Advertising

  2. rodchar

    bruce barker Guest

    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" 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
    bruce barker, Apr 9, 2008
    #2
    1. Advertising

  3. rodchar

    rodchar Guest

    what is this line doing?
    > mycars[mycars.length] = boxes.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').getElementsByTagName('input');
    > for (var i=0; i < boxes.length; ++i)
    > mycars[mycars.length] = boxes.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
    rodchar, Apr 9, 2008
    #3
  4. rodchar

    bruce barker Guest

    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)


    rodchar wrote:
    > what is this line doing?
    >> mycars[mycars.length] = boxes.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').getElementsByTagName('input');
    >> for (var i=0; i < boxes.length; ++i)
    >> mycars[mycars.length] = boxes.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
    bruce barker, Apr 10, 2008
    #4
  5. "bruce barker" <> wrote in message
    news:%...
    > 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);
    >




    --
    Anthony Jones - MVP ASP/ASP.NET
    Anthony Jones, Apr 10, 2008
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Laurent Bugnion

    JavaScript Array Values

    Laurent Bugnion, Oct 23, 2006, in forum: ASP .Net
    Replies:
    4
    Views:
    345
    Barry
    Oct 24, 2006
  2. Mara Guida

    const and array of array (of array ...)

    Mara Guida, Sep 2, 2009, in forum: C Programming
    Replies:
    3
    Views:
    474
    David RF
    Sep 3, 2009
  3. Tom
    Replies:
    3
    Views:
    199
    salsablr
    Dec 20, 2004
  4. Kermit Piper
    Replies:
    4
    Views:
    143
  5. Replies:
    11
    Views:
    173
    Zvt.Fred
    Apr 13, 2009
Loading...

Share This Page