Search code examples
javascriptjqueryhtmlclone

How can I remove clone HTML JQuery


I'm just a beginner I want to create button for remove clone but I can't. This is my code but it isn't work. Please help me to know where did I wrong.

PS. Sorry for my poor English

HTML

 <div class="docu">
      <div class="row">
         <div class="col-sm-2"></div>
            <div class="col-lg-7"  id="Freport" name="Freport">
              <div class="input-group">
                <div class="input-group-btn">
                    <select name="tell" id="tell" class="btn btn-default dropdown-toggle">
                        <option value="0"></option>
                        <option value="1"></option>
                        <option value="2"></option>
                        <option value="3"></option>
                    </select>
      </div><!--End row-->

      <input type="text" class="form-control" aria-label="..." placeholder="...">

      </div><!-- /btn-group -->
    </div><!-- /input-group -->

         <div class="col-sm-1">

<button type="submit" id="btnClonereport"><span class="glyphicon glyphicon-plus-sign" aria-hidden="true"></span></button>
<button type="submit" id="btnDelreport"><span class="glyphicon glyphicon-minus-sign" aria-hidden="true"></span></button>
</div>

<div id="container4">
</div>

JS This is my script I can clone but I can't remove clone.

<script type="text/javascript">
    $(function () {
        $("#btnClonereport").bind("click", function () {


            var index = $("#container4 select").length + 1;

            //Clone the DropDownList
            var fre = $("#Freport").clone();
            var fre2 = $("#orand").clone();


            //Set the ID and Name
            fre.attr("id", "Freport_" + index);
            fre.attr("name", "Freport_" + index);
            fre2.attr("id", "orand_" + index);
            fre2.attr("name", "orand_" + index);


            //[OPTIONAL] Copy the selected value
            var selectedValue = $("#Freports option:selected").val();
            fre.find("option[value = '" + selectedValue + "']").attr("selected", "selected");
            var selectedValue = $("#orands option:selected").val();
            fre2.find("option[value = '" + selectedValue + "']").attr("selected", "selected");

            //Append to the DIV.


            $("#container4").append(fre2);
            $("#container4").append(fre);
            $("#container4").append("<br /><br />");


        });
        $("#btnDelreport").bind("click", function(){
            $(this).closest("#container4").remove();
    });
    });

</script>

Solution

  • So you want to remove all form clones? Try this for your delete button action:

    EDIT: If you want to remove the last occurance, that makes things much simpler.

        $("#btnDelreport").bind("click", function(){
            $('#container4').children('.col-lg-7').last().remove();
        });