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,930
  • 9
  • 33
  • 56

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
  • 15,336
  • 3
  • 31
  • 49