max rox
max rox

Reputation: 29

textarea values are not submitted on enter key

I have tried every possible combination for the textarea to submit the value when enter key is pressed but none of the worked.I cant find the error. plz help.

$('.textarea').keyup(function(e){
            if( e.which == 13 ){
                $('#messageFrm').submit();
            }

        });


        $('#messageFrm').submit(function(){

            var message = $('.textarea').val();

            $.post("handlers/messages.php",{msg:message,condition:"sendMessage"}, function(response){

                if( response==1 ){
                    LoadChat();
                    document.getElementById('messageFrm').reset();
                }

            });

            return false;

        });

here's my jquery script. Actually on pressing enter the data is inserted on the database but the textarea still hve the message and a new line is added with enter key.

Upvotes: 0

Views: 85

Answers (2)

Tu Tran
Tu Tran

Reputation: 458

You can use keydown of jQuery to add a listener for textarea and event.preventDefault() to prevent add a new line.

You can see my pen: https://codepen.io/blogk/pen/vaPYwL?editors=1010

Upvotes: 0

Kunal
Kunal

Reputation: 229

After submitting form all you are doing to clear text area then why not just code to clear value of text area directly

For that just replace

 document.getElementById('messageFrm').reset();

With

 $('.textarea').val('');

Upvotes: 1

Related Questions