Page 1 of 1

append() array fields

Posted: Thu Jul 01, 2010 3:58 am
by wasir
I am using jquery append() to clone a div consisting textfield and checkbox both as separate arrays. Obviously, when posted checkbox will only show if checked.

Is it possible to link the checkbox and textfield with a common key and the key also increment with appended div?
or
is it possible to change the field name every time it's appended?

Hope I am clear in explaining. Below is the code.

Code: Select all

        <script type="text/javascript">
              $(document).ready(function() {
                    $("#addTestAppend").click(function() {
                          $("#testAppend").append(
                                "<div>"
                                      +"<input type=\"text\" name=\"name[]\" /><br />"
                                      +"<input type=\"checkbox\" name=\"needDivTwo[]\" class=\"needDivTwo\" />"
                                +"</div>"
                          );
                    });
              });
        </script>
        <form name="name" method="post" action="formProcess.php">
              <div id=testAppend>
                    <div>
                          <input type="text" name="name[]" /><br />
                          <input type="checkbox" name="needDivTwo[]" class="needDivTwo" />
                    </div>
              </div>
              <input type="button" id="addTestAppend" value="[ Add Append ]"  />
        </form>

Re: append() array fields

Posted: Thu Jul 01, 2010 4:40 am
by Weirdan

Code: Select all


              $(document).ready(function() {
                    var i = 0;
                    $("#addTestAppend").click(function() {
                          $("#testAppend").append(
                                "<div>"
                                      +"<input type=\"text\" name=\"name["+i+"]\" /><br />"
                                      +"<input type=\"checkbox\" name=\"needDivTwo["+i+"]\" class=\"needDivTwo\" />"
                                +"</div>"
                          );
                          i++;
                    });
              });

Re: append() array fields

Posted: Thu Jul 01, 2010 7:01 pm
by wasir
Thanks Weirdan.

Works great!