Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums JavaScript Chat2 Reply To: Chat2

#175515
Ilan Firsov
Participant

Without seeing the code we can only guess, but I think I know what your problem is.
As far as I can tell <input /> tags by default submits the form when you press the enter key while <textarea /> does not.
If you don’t need multiple lines you can just change the <textarea /> to <input /> and everything should be fine
You can force <textarea /> to submit the form with Javascript:

 <form>
      <textarea id="multiline"></textarea>
     </form>
 $('#multiline').on('keypress', function(e) {
      if(e.which == 13) {
        e.preventDefault();
        $('form').submit();
      }
    });

And if you want to have multiline support with let’s say CTRL+Enter, just add this to the keypress callback function:

   if(e.ctrlKey && e.which == 10) { //when ctrl is pressed enter input code is 10 instead of 13
        $(this).append("\n"); // \n = line break
      }

Here is a quick Pen

  • I’m using jQuery here, but it should be fairly easy in vanilla JS as well