title: I am trying to send text- messages on WhatsApp with javascript?
body: <p>I am trying to send text messages on whatsapp web version on chrome.
  (<a href="http://web.whatsapp.com" rel="nofollow noreferrer">www.web.whatsapp.com</a>)</p>
  
  <p>This is the code:</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="false" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>document.getElementsByClassName("input")[1].innerHTML="This message was written via JS script! ";
  
  var input = document.getElementsByClassName("icon btn-icon icon-send");
  input[0].click();</code></pre>
  </div>
  </div>
  </p>
  
  <p>But the problem is , initially when no text is present the input box looks like this:
  <img src="https://i.stack.imgur.com/l9d1j.png" alt="enter image description here"></p>
  
  <p>And only when I physically write some text it changes to this:</p>
  
  <p><img src="https://i.stack.imgur.com/q9C5p.png" alt="enter image description here"></p>
  
  <p>And only now my script works since it requires the <code>Send text button</code>.</p>
  
  <p>I tried Jquery code to simulate keypresses at $('.input)by following function:</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="false" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>function pressKey() {
    var e = jQuery.Event("keypress");
    e.which = 32; // # space
    $(".input").trigger(e)[1];
    e.which = 91;
    $(".input").trigger(e)[1];
    e.which = 32; // # space
    $(".input").trigger(e)[1];
    e.which = 32; // # space
    $(".input").trigger(e)[1];
  
  }</code></pre>
  </div>
  </div>
  </p>
  
  <p>It didn't work.</p>
  
  <p>How can I get the <code>Send text</code> button by script?</p>
  
  <p><a href="https://www.youtube.com/watch?v=9kld1BV1bmg" rel="nofollow noreferrer">Here is  the screen recording</a> : </p>