Add/Retrieve value to HTML array via Javascript

Discussion in 'JavaScript' started by mark84, Apr 1, 2008.

  1. #1
    Hi all,

    In have an hidden HTML array say..

    <input type="hidden" name="gw_type[]" id="gw_type" />
    Code (markup):
    ..now i want add values to the array/object via javascript
    For example i tried this but didnt work..

    var gw_type = document.getElementById("gw_type");
    gw_type.value[0] = "One";
    gw_type.value[1] = "Two";
    alert(gw_type.value[0]);
    Code (markup):
    But it always alerts "undefined".
    Please help me with this.
     
    mark84, Apr 1, 2008 IP
  2. So1

    So1 Peon

    Messages:
    45
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #2
    u can not store array in input.
    Better, do the textarea with visible:none; style and push elements into lines of textarea. Or... If u want to use nput, do as it in following:
    HTML
    <input type="hidden" name="my_arr" id="input_arr" value="" />
    Code (markup):
    JS
    
    var input_arr = document.getElementById('input_arr');
    function _push(elem, target)
    { target.value = target.value + elem + ";"; }
    _push("one", input_arr);
    _push("two", input_arr);
    
    Code (markup):
    after this, your html will be
    <input type="hidden" name="my_arr" id="input_arr" value="one;two;" />
    ; - is a glue. If u want to make the array, use EXPLODE function (php, js).
     
    So1, Apr 3, 2008 IP