javascriptkey-events

press key inside an input box


I am filling up a login form using

     document.getElementById('i0116').value = email;
     document.getElementById('i0118').value = password;
     document.getElementById('idSIButton9').click();

Now the problem starts when form is identifying that the value is not filled by any key event, I mean the placeholders remains there even if I fill the form and on submit it says fields are empty.

I was trying to fix it by firing a keypress event on input box before I fill the value. but I am not able to do it. I tried :

var target = document.getElementById('email');
var evt = document.createEvent("Events");

evt.initEvent("keypress", true, true);

evt.view = window;
evt.altKey = false;
evt.ctrlKey = false;
evt.shiftKey = false;
evt.metaKey = false;
evt.keyCode = 0;
evt.charCode = 'a';

target.dispatchEvent(evt);

also instead of "Events" I tried "UIEVENTS" and "KEYEVENTS" none of them helped , I am using chrome browser.


Solution

  • My Issue was resolved using:

    var element = document.getElementById('idTxtBx_SAOTCC_OTC');    
    var evt = document.createEvent("HTMLEvents");
            evt.initEvent("change", false, true);
            element.dispatchEvent(evt);
    

    It was knockout js, because of which just setting the value of element was not working.so I was trying to keypress. chanage event triggers "textInput" for knockoutjs, Instead of just setting .value attribute.