How to submit – Pointy Sep 08 '19 at 03:19

  • Hi Pointy. On this – mrlemmer11 Sep 08 '19 at 03:21
  • Possible Here is the solution: https://stackoverflow.com/a/49389811/11857699 – Vedat Sep 08 '19 at 03:40
  • @Vedat --- Thank you, I did see this. Given the html I provided above, how would the suggestion in your link handle this part: `event.target.form.dispatchEvent` specifically the `form` part. I do not see what that would point to given the provided html.... thanks in advance. – mrlemmer11 Sep 08 '19 at 03:50
  • @Vedat ---fyi, I have tried exactly as the code show less the variable name change. Also tried `event.target.dispatchEvent` --- neither worked, nor did they generate errors in chrome devtools or in my extension tabs. Not sure how else to change that part to have it apply/solve this situation. – mrlemmer11 Sep 08 '19 at 04:12
  • Did this ever get resolved, having a very similar issue? I think in the same application as you! – Freddy Wetson Feb 28 '20 at 14:30
  • 1 Answers1

    1

    The following should work. You just have to make 1 modification to your current code: You must add an id to your text area. If you copy the code that's included then you can do this.

    There is another issue that this does workaround. If you press enter, it starts a new line. This doesn't work. If you just press enter, it will submit the message. With this, if you press shift+enter, then it will start a new line. If you just press enter, then it will submit it! This means that you can submit multi-line messages and submit the messages with the enter key.

    A detailed walk-through of everything that's happening in the code is included in the form of comments.

    // get the chatbox and set the default value for if the shift key and enter key was pressed
    var chatbox = document.getElementById("chatbox_main"), shiftPressed = false, enterPressed = false;
    // listen for the user to press a key
    chatbox.addEventListener("keydown", function(e) {
      // check if the shift key was pressed and say if it was
      if (e.shiftKey) shiftPressed = true;
      // check if the enter key was pressed
      if (e.keyCode == 13) {
        // prevent the enter key from putting in a new line. If shift it pressed, it will be manually added later
        e.preventDefault();
        // say that the enter key was pressed
        enterPressed = true;
      }
    });
    // listen for the user to let go of a key
    chatbox.addEventListener("keyup", function(e) {
      // check if the shift key or enter key was released
      if (e.shiftKey || e.keyCode == 13) {
        // check if the enter key was pressed, and if it wasn't, then reset the shift pressed value because it was only shift that was pressed
        if (!enterPressed) shiftPressed = false;
        // enter was pressed, so move on
        else {
          // make sure that shift wasn't pressed along with enter
          if (!shiftPressed) {
            // get the input from the chatbox and define if the chatbox should be cleared
            var input = chatbox.value;
    
            // prevent the enter key from being typed into the chatbox
            e.preventDefault();
    
            // run you custom code here!
            alert("You submitted this:\n" + input);
    
            // clear the chatbox
            chatbox.value = "";
    
            // reset the value
            enterPressed = false;
          // shift and enter was pressed, so move on
          } else {
            // shift + enter was pressed, so put in a new line
            chatbox.value += "\n";
            // reset the values and let the enter key get pressed
            enterPressed = false, shiftPressed = false;
          }
        }
      }
    });
    <textarea name="chatbox" maxlength="840" class="chat-box-textarea_2V28W" id="chatbox_main"></textarea>
    VirxEC
    • 998
    • 10
    • 22
    • Hi @VirxEX, I appreciate the time you took to put this as an answer. The way I am reading your code, this looks like I need to actually press the Enter key correct? -- I am trying to find out how to submit data to the textarea using only vanilla javascript, not by requiring me to push enter.... (I thought I my OP was clear on this, however, I will further edit to ensure it is so). – mrlemmer11 Sep 08 '19 at 04:41
    • You said "You must press enter for what you typed to be submitted" – VirxEC Sep 08 '19 at 04:43
    • Correct, the only way data can be submitted by the user is if they press the enter button. If there was a submit button then I would simply invoke the submit button and this wouldn't be an issue. But with no form tags, with no submit button, and the only way which data is submitted is via pressing the ENTER button, I do not know how to make my javascript code automatically submit data on my behalf. I hope this clears things up along with my OP edit. – mrlemmer11 Sep 08 '19 at 04:46
    • Oh! Got it. Sorry for the misunderstanding! – VirxEC Sep 08 '19 at 04:49