javascript associative array

Discussion in 'Javascript' started by markrenn@gmail.com, Sep 26, 2005.

  1. Guest

    is there a way to get the index of an array aside from the values that
    you passed from a form?

    <form name="details">
    <label for="fname">
    first name:
    <input type="text" name="name['firstname']" id="fname" />
    </label>
    <label for="mname">
    middle name:
    <input type="text" name="name['middlename']" id="mname" />
    </label>
    <label for="lname">
    last name:
    <input type="text" name="name['lastname']" id="lname" />
    </label>
    <label for="hnum">
    home number:
    <input type="text" name="num['homenumber']" id="hnum" />
    </label>
    <label for="mnum">
    mobile number:
    <input type="text" name="num['mobilenunber']" id="mnum" />
    </label>
    <label for="fnum">
    fax number:
    <input type="text" name="num['faxnumber']" id="fnum" />
    </label>
    <input type="button" name="showName"
    onclick="display('details','name[]')" />
    <input type="button" name="showNum" onclick="display('details',
    'num[]')" />
    </form>

    so here is my script.(which doesnt really work right)

    function display(the_form,the_element){

    var elts = document.forms[the_form].elements[the_element];
    var elts_cnt = (typeof(elts.length) != 'undefined')? elts.length : 0;
    var param = '';

    if (elts_cnt)
    {

    for (var i in elts)
    {
    param = param + i + "=" + elst + "&";
    } // end for

    param = param.substring(0,(param.length-1));

    } // end if

    alert(param);

    }
    i need to make the param like this

    firstname=joe&middlename=smith&lastname=wilson&......
    , Sep 26, 2005
    #1
    1. Advertising

  2. Randy Webb Guest

    said the following on 9/26/2005 1:29 AM:
    > is there a way to get the index of an array aside from the values that
    > you passed from a form?


    What array? There are no JS Arrays defined in the code you posted. Are
    you using PHP server-side by chance? PHP likes those arrayName[] constructs.

    And, it would be better suited as an object instead of an array.

    If the page is generated, have it generate objects for each section you
    want to keep up with. Then its a matter of going through the object's
    properties and getting what you want. But, there is a simpler way if you
    simply redefine your name/IDs/Labels so that it becomes easier.

    The whole thing begs the question "What are you trying to accomplish
    exactly?"

    --
    Randy
    comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
    Randy Webb, Sep 26, 2005
    #2
    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. desktop
    Replies:
    5
    Views:
    376
    James Kanze
    Jun 26, 2007
  2. Yvon Thoraval
    Replies:
    5
    Views:
    200
    Jason Creighton
    Sep 17, 2003
  3. Kevin
    Replies:
    8
    Views:
    138
    Evertjan.
    Dec 21, 2007
  4. VK
    Replies:
    47
    Views:
    522
    Thomas 'PointedEars' Lahn
    Jul 13, 2005
  5. VK
    Replies:
    36
    Views:
    624
    Martin Honnen
    Aug 3, 2005
Loading...

Share This Page