var messages = $(".messages-content"); function insertMessage() { var msgText = $(".action-box-input").val(); if ($.trim(msgText) == "") { return false; } var msg = $("
").addClass("message personal").text(msgText); $("
").addClass("timestamp").text(getTimestamp()).appendTo(msg); msg.appendTo($('.messages-content')); $(".action-box-input").val(null); $.post("/get_abbreviation", { acronym: msgText }, function (data) { var responseMsg = $("
").addClass("message").text(data.response); $("
").addClass("timestamp").text(getTimestamp()).appendTo(responseMsg); responseMsg.appendTo($('.messages-content')); updateScrollbar(); }); } function updateScrollbar() { messages.scrollTop(messages[0].scrollHeight); } function getTimestamp() { var d = new Date(); return d.getHours() + ":" + (d.getMinutes() < 10 ? '0' : '') + d.getMinutes(); } $(window).on('keydown', function (e) { if (e.which == 13) { insertMessage(); return false; } }); $(window).on('load', function () { messages.mCustomScrollbar(); });