0

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');
        }
    });
});
ozil
  • 6,001
  • 8
  • 28
  • 50

1 Answers1

0

Use event delegation for created dymanically dom elements.

$('#tags').on('keypress', 'span[id="newtag"]', function(k){
   if (k.which == 13 || k.which == 32) {
       $(this).append('gfskjsokdfla');
   }
});
Sudharsan S
  • 14,830
  • 3
  • 27
  • 47