Add full div with all its elements using jquery

Using jQuery, I want to add a whole div when a click event occurs. I tried, but it does not work.

When the user clicks the button Add another image, I want to add the entire div tag with all its elements.

Here is my code:

<form method="post" id="form_property" enctype="multipart/form-data">
    <div class="row">
        <div class="span4" id="image">
             <h3><strong>4.</strong> <span>Add Images to your Property</span></h3>

            <div class="fileupload fileupload-new control-group" data-provides="fileupload">
                <label class="control-label" for="inputPropertyPrice">Image files</label>
                <div class="input-append">
                    <div class="uneditable-input"> <i class="icon-file fileupload-exists"></i>
 <span class="fileupload-preview"></span>

                    </div> <span class="btn btn-file">
                                        <span class="fileupload-new">Select file</span>
 <span class="fileupload-exists">Change</span>

                    <input type="file" name="files1" accept="image/*" />
                    </span>
                </div>
            </div>
        </div>
        <!-- /.span4 -->
    </div>
    <!-- /.row -->
    <br/> <a id="another_image" class="btn btn-primary btn-small list-your-property" href="#">Add Another Image</a>
    <br/>
    <br/>
    <input type="submit" name="submit" value=" Save images " class="btn btn-primary btn-large" style="float: left; width: 370px; height: 50px; margin-top: 10px">
</form>

Jquery

    <script type="text/javascript">

    $(document).ready(function(){
        var count = 2;
        $('#another_image').click (function(){

            if(count < 7){
                $('<br/><br/>
                            <div class="input-append">
                                <div class="uneditable-input">
                                    <i class="icon-file fileupload-exists"></i>
                                    <span class="fileupload-preview"></span>
                                </div>
                                                    <span class="btn btn-file">
                                                        <span class="fileupload-new">Select file</span>
                                                        <span class="fileupload-exists">Change</span>
                                                        <input type="file" name="files'+ count +'" accept="image/*" />
                                                    </span>



                            </div>
                        ').appendTo ("#image");


            count++;
            }
        });
        });
    </script>

Can someone please help me fix this?

+4
source share
2 answers

Use .appendTo()with clone().

If you are not using clone(), the same div will be replaced and nothing will be updated in the target. You do not need to enter / copy all html.

Syntax: $(Element_Selector).appendTo(TargetSelector).


$($('.input-append').first().clone()).appendTo("#image");

Fiddle

* . html . .

+10

@Shaunak , , ,

$(document).ready(function(){
        var count = 2;
        $('#another_image').click (function(){

            if(count < 7){
                $('<br/><br/>' +
                  '<div class="input-append">' +
                  '<div class="uneditable-input">'+
                  '<i class="icon-file fileupload-exists"></i>' +
                  '<span class="fileupload-preview"></span>' +
                  '</div><span class="btn btn-file">'+
                  '<span class="fileupload-new">Select file</span>'+
                  '<span class="fileupload-exists">Change</span>' +
                  '<input type="file" name="files'+ count +'" accept="image/*" />'+
                  '</span></div>').appendTo ("#image");
            count++;
            }
        });
});
+4

All Articles