7

I need to trigger or say dispatchEvent KeyPress inside an Input box, I have tried a lot with "initKeyboardEvent" , "initKeyEvent" , "createEvent", even I found similar question's answers but nothing seems to work, neither in firefox, nor in chrome (I think they may be deprecated), as KeyboardEvent.initKeyEvent(), is deprecated.

like we have

document.getElemntByID('button').click();

I want to do something like:

document.getElemntById('email').keyPress(charCode('a'));

but unfortunately I am not able to make keypress work in any way.

I know it is possible in jQuery , but I want to do it in pure javascript, after all jquery uses Javascript in backend , so there must be a way , if I am not wrong.

====Update=========
I wonder

 window.addEventListener('keydown', keyDown(), true);

function keyDown(){
alert('keydown');
}

var fireOnThis = document.getElementById('pwph');
if( window.KeyEvent ) {
    var evObj = document.createEvent('KeyEvents');
    evObj.initKeyEvent( 'keydown', true, true, window, false, false, false, false, 72, 0 );
} else {
    var evObj = document.createEvent('UIEvents');
    evObj.initUIEvent( 'keydown', true, true, window, 1 );
  evObj.keyCode = 72;
}
fireOnThis.dispatchEvent(evObj);

This code returns me true , and even the eventListner is catching this event, then why am I not able to see any text inside the input box? ====================update============================
This seems to work for everyone, but why does it leaves my text field empty, even after returning true?

// Create the event
var evt = document.createEvent( 'KeyboardEvent' );

// Init the options
evt.initKeyEvent(
             "keypress",        //  the kind of event
              true,             //  boolean "can it bubble?"
              true,             //  boolean "can it be cancelled?"
              null,             //  specifies the view context (usually window or null)
              false,            //  boolean "Ctrl key?"
              false,            //  boolean "Alt key?"
              false,            //  Boolean "Shift key?"
              false,            //  Boolean "Meta key?"
               9,               //  the keyCode
               0);              //  the charCode

// Dispatch the event on the element
el.dispatchEvent( evt );
8
  • charCode('a') will invoke the function... You need to pass function expression... Commented Apr 25, 2016 at 13:29
  • How about adding the onkeydown event to the DOM Element? Like: <input type="text" onkeydown="function()"> Commented Apr 25, 2016 at 13:34
  • 1
    Possible duplicate of Invoking KeyPress Event Without Actually Pressing Key Commented Apr 25, 2016 at 13:34
  • Input element is not my hand :) Commented Apr 25, 2016 at 13:35
  • @Boratzan yea may be but that code does not work for me, I wonder why..returns me true but nothing happens to the input field ,it is still empty Commented Apr 26, 2016 at 10:55

3 Answers 3

5

I was trying to dispatch a keyPress event inside an Input element to pass the data-bind condition(KnockOut js) event , but unfortunately KeyPress didn't worked for me, Instead I used change event in place of it.

so I did this:

element = document.getElementById('idTxtBx_SAOTCS_ProofConfirmation');

    element.value = '8885';   // this alone was not working as keypress.

    var evt = document.createEvent("HTMLEvents"); 
        evt.initEvent("change", false, true); // adding this created a magic and passes it as if keypressed
        element.dispatchEvent(evt);

so .value and change event together made, fake inputText or keypressed event successful.

Sign up to request clarification or add additional context in comments.

Comments

0

i think so like you want to add event listener on input box

           var element = document.getElementById("email");

            document.getElementById("email").addEventListener("keypress", function() {
                console.log("keypresh")

            })
            var evt = document.createEvent("KeyboardEvent");
            evt.initEvent("keypress", false, true);
            // adding this created a magic and passes it as if keypressed
            element.dispatchEvent(evt);

2 Comments

addEventListner is something which Listens when an event is fired,I don't want to Listen , I want to Fire or dispatch an event (keypress), on an Input field.
I have gone through this Link already, Mouse events works but not keyboard events: var keyEvent = new KeyboardEvent("keydown", {key : "a", char : "a", shiftKey: true}); document.getElementById('email').dispatchEvent(keyEvent); returns true , but does not enter any character in the input box whose ID is given.
0

initEvent is deprecated or about to be removed. Instead, you can use it like this:

element = document.getElementById('idTxtBx_SAOTCS_ProofConfirmation');
element.value = '8885';   // this alone was not working as keypress.

const evt = new Event("change", {
    bubbles: false,
    cancelable: true
});

element.dispatchEvent(evt);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.