Key press dont work on dynamicly created element [duplicate]

This question already has an answer here:

i am trying to add a new span of tag inside tags div.
my problem is that the code under span[id="newtag"] doesnt
work. how can i make this code work?

            $(document).ready(function () {
            $('#tags').on("click", function () {
                    $(this).append('<span id="newtag" contenteditable="true"></span>');
                    $(this).children(":last").focus();
            });
            $('span[id="newtag"]').keypress(function(k){
                if (k.which == 13 || k.which == 32) {
                    $(this).append('gfskjsokdfla');
                }
            });
        });


Source: New Questions

Leave a Reply