0

I am trying to append an element to a div panel with out it duplicating by 2. It works fine if I append to the body, but If I append to a div it adds 2 more.

$("#click").click(function () {
    $("#panel1").append(txt_box='<div id="sample" id="input_'+id+'" ><img src="assets/img/bluesticky4.png"/><img src="addClick/remove.png" class="remove"/></div>');
    $("#button_pro").append(txt_box);
    id++;

});


var id=1,txt_box;
    $('#button_pro').on('click','.add',function(){
        $(this).remove();
        txt_box='<div id="sample" id="input_'+id+'" ><img src="assets/img/bluesticky.png"/><img src="addClick/remove.png" class="remove"/></div>';
        $("#button_pro").append(txt_box);
        id++;
    });

    $('#button_pro').on('click','.remove',function(){
        var parent=$(this).parent().prev().attr("id");
        var parent_im=$(this).parent().attr("id");
        $("#"+parent_im).slideUp('medium',function(){
            $("#"+parent_im).remove();

        });
    });


});
</script>
Rowel
  • 115
  • 2
  • 11
user2557504
  • 215
  • 3
  • 15
  • can you add a Fiddle or Plunkr? – sjm Jul 08 '15 at 00:19
  • http://jsfiddle.net/v0nni/eba6m6dv/ – user2557504 Jul 08 '15 at 00:24
  • I got the duplication to stop. Now i only need the remove() to work. – user2557504 Jul 08 '15 at 00:42
  • Is this what your trying to achieve: http://jsfiddle.net/sjmcpherso/z8wL6dwk/ the selector in $(selector).on() must exist to work, you can use the secondary selector to target the dynamically included element $(selector).on('click','secondary-selector',function(){}) – sjm Jul 08 '15 at 01:08
  • Yes! Thanks--exactly! Not sure if I can give you a check mark since someone said this answer is already answered – user2557504 Jul 08 '15 at 10:45

0 Answers0