How to get multiple text box array value after submit form in js

Discussion in 'JavaScript' started by Subikar, Sep 23, 2009.

  1. #1
    Here is my code and I am trying to get. Please help me asap.


    <script>

    function myworkhistory(thisobj)
    {
    alert(thisobj.box[1].value);
    return false;
    }

    </script>


    <form name="workhistory" id="workhistory" action="" method="post" onsubmit="return

    myworkhistory(this);">
    <input type="text" name="box[1]" value="" />
    <input type="text" name="box[2]" value="" />
    <input type="text" name="box[3]" value="" />

    <input name="submit" type="submit" value="submit" border="0" />

    </form>
     
    Subikar, Sep 23, 2009 IP
  2. MaceWin

    MaceWin Peon

    Messages:
    38
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Try something like:

    function myworkhistory()
    {
    var val1, val2, val3;

    val1 = document.getElementById('box1').value;
    val2 = document.getElementById('box2').value;
    val3 = document.getElementById('box3').value;

    alert('box1 = ' + val1 + ',box2 = ' + val2 + ',box3 = ' + val3);
    return false;
    }

    and rename box[1], box[2], box[3] to box1, box2, box3
     
    MaceWin, Sep 26, 2009 IP
  3. camjohnson95

    camjohnson95 Active Member

    Messages:
    737
    Likes Received:
    17
    Best Answers:
    0
    Trophy Points:
    60
    #3
    
    var maxBoxes = 3;
    for(i=1;i<=maxBoxes;i++) {
             var boxValue = document.getElementById("box"+i).value;
             alert("Box" + i + " = " + boxValue);
    }
    
    Code (markup):
     
    camjohnson95, Sep 26, 2009 IP