-1

I have one script where I want to add Return when shift+enter is pressed, it is working fine with single Enter, but it is not adding shift+Enter with \n due to error of javascript is not selecting exact content. I am getting following error

Uncaught TypeError: Cannot read property 'substring' of undefined
    at HTMLFormElement.<anonymous> (86:206)
    at HTMLFormElement.dispatch (jquery.self-bd7ddd393353a8d2480a622e80342adf488fb6006d667e8b42e4c0073393abee.js?body=1:5227)
    at HTMLFormElement.elemData.handle (jquery.self-bd7ddd393353a8d2480a622e80342adf488fb6006d667e8b42e4c0073393abee.js?body=1:4879)

Here is my codes

$(document).ready(function(){
  $('.new_message').keyup(function (event) {
  if (event.keyCode === 13 && !event.shiftKey) {
    event.preventDefault();
      var values = $(this).serializeArray();
      App.conversation.speak(values);
      $(this).trigger('reset');
  } else {
    if(event.keyCode === 13 && event.shiftKey){
      var content = $(this).serializeArray();
      var caret = getCaret($(this));
      this.value = content.substring(0, caret - 1) + "\n" + content.substring(caret, content.length);
    }
  }
  });
});

I just want to add enter when user presses shift+enter and if he presses normal enter, it is working fine and sending text message. I am using rails with socket to send chat messages to user.

Kamal Panhwar
  • 2,345
  • 3
  • 21
  • 37
  • 1
    `content.substring` is the first time `content` appears in the code you've shared. Why shouldn't it be `undefined`? – Quentin Jan 09 '20 at 08:00
  • Sorry content = values, I was playing with codes to resolve issue. actualy content = $(this).serializeArray() , I also tried content = this but it is not working. – Kamal Panhwar Jan 09 '20 at 08:04
  • Well now `content` is the return value of `serializeArray()`. So its an array. So why should it have a `substring` method? It isn't a string. – Quentin Jan 09 '20 at 08:05

3 Answers3

0
  var content = $(this).serializeArray();
  var caret = getCaret($(this));
  this.value = content.substring(0, caret - 1) + "\n" + content.substring(caret, content.length);

To get the value of the textarea the event is firing on, use this.value (just like you do in the third of the lines I quoted).

serializeArray() is for creating an array containing all the data in a form. It won't return a string, so thee return value won't have a substring method.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • @KamalPanhwar — That seems unlikely, but its impossible to test because you didn't provide a [mcve] – Quentin Jan 09 '20 at 09:20
0

The issue was I am using .new_message form selector, although it is getting value but it is now allowing me to substring method. now I have changed by adding another id to message_box and change code to following.

$(document).ready(function(){
  $('.new_message').on('keyup', function(event) {
  if (event.keyCode === 13 && !event.shiftKey) {
    event.preventDefault();
      var values = $(this).serializeArray();
      App.conversation.speak(values);
      $(this).trigger('reset');
  } else {
    if(event.keyCode === 13 && event.shiftKey){
      var content = $('#message_box').val()
      var caret = getCaret(this);
      $('#message_box').val( content.substring(0, caret - 1) + "\n" + content.substring(caret, content.length) );
    }
  }
  });
});

It worked, but I noticed it was adding two Returns and it was on beginning, so now I removed second condition and it is working perfectly. Thank you for your support.

Kamal Panhwar
  • 2,345
  • 3
  • 21
  • 37
-1
Uncaught TypeError: Cannot read property 'substring' of undefined

This error means tha you are calling a function substring on a variable that doesn't exist. In your case it's variable content in the else statement. Make sure that this variable exists and is accessible within the scope of that function.

[Edit]

Now that you have updated your code and changed variable values to content I refuse to believe you get the same error. Please review your code and update the question to reflect the actual state of the application.

Nightray
  • 93
  • 3