Forums

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

Home Forums JavaScript Chat2

  • This topic is empty.
Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #174768
    Jared_S
    Participant

    I followed the tutorial on Chat2 and I can’t send the message. I press enter and it just adds another line to the text box. What is going on and what can I do to fix it?

    Chat2 Tutorial

    #175429
    noahgelman
    Participant

    Unless we can see your code, we can’t help

    #175430
    noahgelman
    Participant

    Do you have a live code example of what you’ve tried to implement?

    #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
Viewing 4 posts - 1 through 4 (of 4 total)
  • The forum ‘JavaScript’ is closed to new topics and replies.