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?

    1 answer 1

    It would be nice to see the form, but presumably like this:

     document.addEventListener("DOMContentLoaded", function() { var input = document.getElementById('chatMessage'); input.value = 'Сообщение'; var event = new KeyboardEvent('keydown', { 'view': window, 'bubbles': true, 'cancelable': true, 'which': 13 }); input.dispatchEvent(event); }); 
     <form> <input id="chatMessage" onKeydown="alert('Clicked!')"> </form> 

    • On the country there is no button to send messages, there is only an input field, and to send messages only by pressing enter, so I want to simulate something like pressing the enter key - SloGS
    • Not a problem either. Changed the answer. - Sergey Gornostaev
    • csgodouble.com That's actually the site, the message for some reason does not send, still does not reach me, the console gives true and nothing more - SloGS
    • Sending occurs not on enter'u in input'e, but on the event submit'a form. Therefore, KeyboardEvent('keydown') must be replaced with Event('submit') , the parameter which to remove and dispatchEvent(event) be called for the form. - Sergey Gornostaev
    • var input = document.getElementById('chatMessage'); input.value = 'Сообщение'; var event = new Event('submit', { 'view': window, 'bubbles': true, 'cancelable': true }); form.dispatchEvent(event); I'm sorry, I can't understand, I'm just learning, but this function is very necessary for me (( - SloGS