append() array fields

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
wasir
Forum Commoner
Posts: 49
Joined: Sun Jul 08, 2007 11:28 pm

append() array fields

Post 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>
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Re: append() array fields

Post 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++;
                    });
              });
wasir
Forum Commoner
Posts: 49
Joined: Sun Jul 08, 2007 11:28 pm

Re: append() array fields

Post by wasir »

Thanks Weirdan.

Works great!
Post Reply