I wrote a Google Chrome extension and the site that I want to use my extension on requires me to click or tab onto a text box (because I think it runs javaScript verification "onClick" only). I can get the text in the box with my extension using:
document.getElementById("input1").value = 'test';
But when I click submit, it thinks I did not enter anything in the "input1" text box because I never clicked it or tabbed on it.
Can someone help me get around this?
For simulating keyboard events in Chrome:
There is a related bug in webkit that keyboard events when initialized with initKeyboardEvent get an incorrect keyCode and charCode of 0: https://bugs.webkit.org/show_bug.cgi?id=16735
A working solution for it is posted in this SO answer.
Simulating a mouse click
My guess is that the webpage is listening to mousedown rather than click (which is bad for accessibility because when a user uses the keyboard, only focus and click are fired, not mousedown). So you should simulate mousedown, click, and mouseup (which, by the way, is what the iPhone, iPod Touch, and iPad do on tap events).
To simulate the mouse events, you can use this snippet for browsers that support DOM 2 Events. For a more foolproof simulation, fill in the mouse position using
initMouseEvent
instead.When you fire a simulated click event, the browser will actually fire the default action (e.g. navigate to the link's href, or submit a form).
In IE, the equivalent snippet is this (unverified since I don't have IE). I don't think you can give the event handler mouse positions.
Simulating keydown and keypress
You can simulate keydown and keypress events, but unfortunately in Chrome they only fire the event handlers and don't perform any of the default actions. I think this is because the DOM 3 Events working draft describes this funky order of key events:
This means that you have to (while combing the HTML5 and DOM 3 Events drafts) simulate a large amount of what the browser would otherwise do. I hate it when I have to do that. For example, this is roughly how to simulate a key press on an input or textarea.
I don't think it is possible to simulate key events in IE.
you can raise the click event on an element by doing
Example here
Or even shorter, with only standard modern Javascript:
The
new MouseEvent
constructor takes a required event type name, then an optional object (at least in Chrome). So you could, for example, set some properties of the event:Focus might be what your looking for. With tabbing or clicking I think u mean giving the element the focus. Same code as Russ (Sorry i stole it :P) but firing an other event.