0

I am building a reply system like on facebook where user can reply to another user's comment

So I have this jquery code that loads a new reply above an existing list.

$(".send-comment").on('keypress',function(event) {
        var keycode = (event.keyCode ? event.keyCode : event.which);
        if (keycode == '13') {
            var table = $(this).closest("table");
            var commenttext = $(this);
                $.ajax({
                    url: '/newcomment',
                    type: "POST",
                    data: {
                        comment: $(commenttext).val(),
                    },
                    success: function(response) {
                        $(response).insertAfter(table);
                        $(commenttext).val("");
                    }
                });
        }
    });

Problem is the $(".send-comment").on('keypress',function(event) {...}) doesn't effect on the new content loaded with ajax while the other events work on it like this one $(document).on("click", ".see-replies", function(e) {...}) that I use to see replies list.

I tried this two versions but nothing works

$(document).on('keypress',".send-comment",function(event) {...})

$(document).keypress(".send-comment",function(event) {...})

$(".send-comment").keypress(function(event) {...})

Thank you in advance for the help.

lucasboko
  • 139
  • 1
  • 2
  • 9
  • `$(document).on('keypress',".send-comment",function(event) {...})` should work. are the classes definitely on the new content? – BenG Oct 30 '15 at 19:47
  • http://stackoverflow.com/questions/16122380/trigger-keypress-with-jquery – Matt R Oct 30 '15 at 20:54

0 Answers0