Hello, there is a site with such a pollevom water messages in chat.

<form id="chatForm"><input class="form-control" placeholder="Type here to chat..." id="chatMessage" maxlength="200" autocomplete="off" type="text"></form> 

I need to send a message from me to the chat at the entrance to the site. Without reloading the page. Because when sending a message, then you still need to press the button ok. As confirmation of sending. Those. submitting a form using sumbit. Not coming Because after the sambit the page immediately reloads.

And if you manually write a message and press enter, the message is sent and the page does not reload. Immediately I will say the site is not mine. I want to make an extension.

So I fill in the field to send

 document.getElementById('chatMessage').value = 'Сообщение'; 

And you need to automate the sending.

How can I implement sending a message for this field by simulating pressing the enter button?

    3 answers 3

     <script> var form = document.querySelector('#chatForm'); form.onsubmit = function(event){ event.preventDefault(); var message = document.querySelector('#chatMessage').value; } </script> 

    In the variable message - your message text, then use the Ajax to send it

      If the reboot is not needed, then the Ajax will help you!

      • Specifically, you can not give an example? he is not very strong in this, but all the same is interesting - SloGS
       <script type="text/javascript"> document.YOUR_FORM.submit(); </script> 

      Those. you look for your form in any way you like (by class, id, etc.)), and call the submit () method on it;

      For example:

       <form id="chatForm" action="YOUR_URL" name="YOUR_FORM"> <input class="form-control" placeholder="Type here to chat..." id="chatMessage" maxlength="200" autocomplete="off" type="text"/> </form> <script type="text/javascript"> document.YOUR_FORM.submit(); </script> 

      Those. You need the following:

      1) We have the form.

      2) Enter the data.

      3) Press enter.

      4) The data goes away. The page does not reload.

      5) Buns in the appendage (for example, cleaning all or part of the form).

      I will not write the code, because JavaScript is not mine, but I'll tell you how to do it and where to look:

      1) You make a form.

      2) Enter the data.

      3) Press enter.

      3.1) On js you intercept the standard submit behavior, cancel it.

      3.2) You take the data and send it using ajax, there may be more ways, but it will be easier for you to deal with ajax.

      4) The data goes away.

      4.1) With ajax, you can get an answer (they say, data is received, everything is good / bad and respond, update data, etc.)

      5) You can clear the form or do something else useful.

      • No, just like submit () reloads the page, I forgot to specify that I need to send a message to the chat without reloading the page, just as if I pressed the enter button. - SloGS
      • If you have a form and press the Enter button, the page will reload and this is the expected response. - Eugene
      • No, the site is not mine, and when I click on the enter page does not reload, I just need to make an imitation of pressing the enter button in this field? as if without sending a form or something - SloGS
      • You'd better rewrite the question in more detail. The fact that sending data is understandable. But, you most likely need to do this after a certain time interval, right? Or reacting to any other events. The site is not yours, i.e. you don't have access to the sources, right? Correcting your question more correctly and giving more information will be easier to answer, and now poking your finger into the sky ... With all the desire to help. - Eugene
      • Wrote in more detail. Read it. Hopefully and more understandable. - SloGS