2

i am trying to trigger keypress event using pure javascript from console but unable to do so.

below is my code

var abc = document.querySelector("#main > footer > div.block-compose > div.input-container > div > div.input");

Code 1:

function fire( elem, type ) {

  var evt = elem.createEvent("Events");

  evt.initEvent( type, true, true, window, 1);

  elem.dispatchEvent(evt);

}

document.addEventListener( "plop", function() {
   console.log( "Fired a synthetic click event" );
}, false );

fire( abc, "plop" );

Code 2

var event = document.createEvent("KeyboardEvent");

event.initKeyboardEvent("keypress", true, true, null, false, false, false, false, 115, 0);

Any suggestion??

Jsfiddel link:

http://jsfiddle.net/mishragaurav31/VLZGk/1105/

i just want javascript code that can alert "keypressed"

gaurav
  • 237
  • 4
  • 16

1 Answers1

0

How about this (sending the key with an onkeydown call): jsBin

A.D
  • 2,150
  • 3
  • 13
  • 19
  • 1
    Please don't simply link to external resources. It's fine to demo your answer, but always imagine how good your answer is if your stripped all non stack overflow links from your answer. – grepsedawk Jun 02 '17 at 14:33