trouble accessing form array values

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Samishii23
    New Member
    • Sep 2009
    • 246

    trouble accessing form array values

    So I have a dynamically created form.
    This is a quick run down of what happens.

    Page Loads -> jQuery Ajax load page -> Another Ajax loads form:
    Code:
    <form id="listForm" name="listForm">
    <input type="text" name="listQty[]" value="0">
    <input type="checkbox" name="listCb[]" value="php id #">
    <input type="hidden" name="listTitle[]" value="BlahBlah">
    (Up to 7 more sets of items currently)
    </form>
    I found this method:
    Code:
    var arrayElements = document.listForm.elements["listQty[]"];
    When I try to loop through the values of each element, it just gives me 0 everytime even if I put something else into the text box.

    What am I ( and that website... ) doing wrong?
  • Brian Connelly
    New Member
    • Jan 2011
    • 103

    #2
    Not sure about this...Im learning. But if you remove the value 0 from the array. Does it work? I believe that you assigned the array a 0 memory location, but if you remove that value, each time you enter a value in the form it will append an new index.

    Comment

    • Rabbit
      Recognized Expert MVP
      • Jan 2007
      • 12517

      #3
      I'm not sure what you're trying to do but if you're trying to do but if you're trying to return the array of form elements, then you need to stop at .elements. The rest of it will return only one element.

      If you are trying to return the value of the element, then you need .value.

      Comment

      • Samishii23
        New Member
        • Sep 2009
        • 246

        #4
        Sorry, I'll clarify a little bit. This is what I'm doing:
        Code:
        var eleArray = document.formList.elements["listQty[]"];
        
        for ( var i in eleArray ) {
          alert(i.value); // undefined
          alert(i); // 0
          }

        Comment

        • Rabbit
          Recognized Expert MVP
          • Jan 2007
          • 12517

          #5
          Well, listQty[] isn't an array. It's the name of a textbox so it has no elements. Just use document.formLi st.elements.

          Comment

          • Samishii23
            New Member
            • Sep 2009
            • 246

            #6
            Going by my last post, am I not?

            Comment

            • Rabbit
              Recognized Expert MVP
              • Jan 2007
              • 12517

              #7
              No, you're not. You're using document.formLi st.elements["listQty[]"], which will return the textbox, which has no elements. You need to use document.formLi st.elements, which will return the array of elements in formList.

              Comment

              • Samishii23
                New Member
                • Sep 2009
                • 246

                #8
                I would say that would be fine if only "listQty[]" was in the form, but I will have 3 others like the previously metioned. Doesn't form.elements[] if given a string return all the elements matching?

                Comment

                • Dormilich
                  Recognized Expert Expert
                  • Aug 2008
                  • 8694

                  #9
                  true, but your loop is wrong. for looping through a list you use a standard for() loop, not the for … in loop. further, i refers to the key, which is either a string or a number.
                  Code:
                  var ele, eleList = document.formList.elements["listQty[]"];
                  for (var i = 0, l = eleList.length; i < l; i += 1) {
                      ele = eleList[i];
                      // …
                  }

                  Comment

                  • Rabbit
                    Recognized Expert MVP
                    • Jan 2007
                    • 12517

                    #10
                    I see, I thought you were trying to return all the form elements but you were trying to return all form elements with the same name.

                    Comment

                    Working...