5
I’m creating a chat for a website and need the user to just press the enter and the message appears on chat, this function works normally with a field textarea but with input nay.
HTML
<form action="" method="post" id="frm-msg">
    <fieldset>
        <label> 
            <input type="text" name="mensagem" id="mensagem" class="textarea" />
        </label> 
    </fieldset>
</form>
Javascript
$(function() {
     //inserir
     $("#submit").click(function() {
         var msg = $("#mensagem").val();
         msg = $.trim(msg);
         if (msg != '') {
             $.post('chat.php', {
                 mensagem: msg,
                 acao: 'inserir'
             }, function(retorno) {
                 $("#painel").prepend(retorno);
                 $("#mensagem").val('');
             });
         }
     });
     //atualizar
     setInterval(function() {
         $.post('chat.php', {
             acao: 'atualizar'
         }, function(retorno) {
             $("#painel").html(retorno);
         });
     }, 5000);
     $(document).keypress(function handleEnter(e, func) {
         if (e.keyCode == 13 || e.which == 13) {
             var msg = $("#mensagem").val();
             msg = $.trim(msg);
             if (msg != '') {
                 $.post('chat.php', {
                     mensagem: msg,
                     acao: 'inserir'
                 }, function(retorno) {
                     $("#painel").prepend(retorno);
                     $("#mensagem").val('');
                 });
             }
         }
     });
 });