jQuery how to send via onkeyup and continue with the selected input

Asked

Viewed 51 times

0

I wanted to know how to send the message and the input continue selected so I can continue writing.

My code is:

$(document).on('onkeyup', '.input', function(e) {

        var message = $(this).val('');
           if (e.keyCode == 13 && message != ''  && projeto_id != '') {
               $(this).val('')

               var dataStr = "&message=" + message + '&projectId=' + projeto_id;
               $.ajax({
                   type: "POST",
                   url: "/sendMessage",
                   data: dataStr,
                   cache: false,
                   success: function(data) {

                   },
                   error: function (jqXHR, status, err) {

                   }, 
                   complete: function() {

                   }
               })
           }
  • 1

    onkeyup is wrong, the correct is keyup; var message = $(this).val(''); is wrong, the correct is var message = $(this).val();

1 answer

0

Your code has three errors:

  • the name of the event is keyup, nay onkeyup.
  • to return the field value .inpup, use the method .val() with no argument;
  • the call $(this).val('') is clearing the field.

Your code should look like this:

$(document).on('keyup', '.input', function(e) {
  var message = $(this).val();
  const projeto_id = '1234'
  if (e.keyCode == 13 && message != '' && projeto_id != '') {
    // $(this).val('') // << isso define o campo como uma string vazia
    var dataStr = "&message=" + message + '&projectId=' + projeto_id;


    //...
  }
})

https://jsfiddle.net/2q4n9pxu/

Browser other questions tagged

You are not signed in. Login or sign up in order to post.