1

if user click <li><a id="print" href="">출력 하기</a></li> it's id ="print" so it makes id="print2" button, title is 복사하기. and makes checkbox.

and then if user click id="print2" button "복사하기" it doesn't work. there is no reaction.

what do i miss?

 $(document).ready(function(){
        $("#print").unbind('click');
        $("#print").on('click', function(ev){
            $('#main').prepend('<center><button class="btn btn-primary btn-lg" id="print2">복사하기</button></center>')
            $('.post').prepend('<input type="checkbox" />');
            ev.preventDefault();
        });


    $("#print2").on('click', function(){
        var images ='';
        $('li').each(function(){
            var thisCheckFlag=$(this).children('input[type="checkbox"]').is(':checked');
            if(thisCheckFlag){
                images+='<img src ="'+$(this).find('img').attr('src')+'">';
            }
        });
        if(images){
            var myWindow=window.open('','printWindow','width=800,height=800');
            myWindow.document.write(
                '<html><head><title>Print</title></head><body>'
                +images+'</body></html>'
            );
            myWindow.focus();
            myWindow.print();
        }
        else alert('먼저 선택하세요.');
    });


    });
박형렬
  • 323
  • 3
  • 20
  • Possible duplicate of [Event binding on dynamically created elements?](http://stackoverflow.com/questions/203198/event-binding-on-dynamically-created-elements) – juvian Feb 14 '17 at 15:09

2 Answers2

1

Your #print2 is not defined when you are attaching the click to it. You need to delegate the click to a parent, typically the document.. See more

So your function will become:

$(document).on('click', '#print2', function(){
   //rest of code.
});
callback
  • 3,144
  • 1
  • 23
  • 47
0

The problem here is that the event will not bind with the dynamically created elements,
So whatever you add a new button you need to add the event associated to it so :

$(document).ready(function() {
    $("#print").unbind('click');
    $("#print").on('click', function(ev) {
        $('#main').prepend('<center><button class="btn btn-primary btn-lg" id="print2">복사하기</button></center>')
        $('.post').prepend('<input type="checkbox" />');
        $("#print2").on('click', function() {
            var images = '';
            $('li').each(function() {
                var thisCheckFlag = $(this).children('input[type="checkbox"]').is(':checked');
                if (thisCheckFlag) {
                    images += '<img src ="' + $(this).find('img').attr('src') + '">';
                }
            });
            if (images) {
                var myWindow = window.open('', 'printWindow', 'width=800,height=800');
                myWindow.document.write(
                    '<html><head><title>Print</title></head><body>' + images + '</body></html>'
                );
                myWindow.focus();
                myWindow.print();
            } else alert('먼저 선택하세요.');
        });
        ev.preventDefault();
    });


});
Ryad Boubaker
  • 1,491
  • 10
  • 16