Note that there are some explanatory texts on larger screens.

plurals
  1. POSimulating a tab keypress using JavaScript
    text
    copied!<p>I'd like to have the browser act as if the user had pressed the Tab key when they click on something. In the click handler I've tried the following approaches:</p> <pre><code>var event = document.createEvent('KeyboardEvent'); event.initKeyEvent("keypress", true, true, null, false, false, false, false, 9, 0); this.input.focus()[0].dispatchEvent(event); </code></pre> <p>And jQuery:</p> <pre><code>this.input.focus().trigger({ type : 'keypress', which : 9 }); </code></pre> <p>...which I took from <a href="https://stackoverflow.com/questions/596481/simulate-javascript-key-events">here</a>.</p> <p>The first approach seems to be the best bet, but doesn't quite work. If I change the last two parameters to 98, 98, indeed, a 'b' is typed into the input box. But 9, 0 and 9, 9 (the former of which I took right from the MDC web site) both give me these errors in firebug under FF3:</p> <pre><code>Permission denied to get property XULElement.popupOpen [Break on this error] this.input.focus()[0].dispatchEvent(event); Permission denied to get property XULElement.overrideValue [Break on this error] this.input.focus()[0].dispatchEvent(event); Permission denied to get property XULElement.selectedIndex [Break on this error] this.input.focus()[0].dispatchEvent(event); Permission denied to set property XULElement.selectedIndex [Break on this error] this.input.focus()[0].dispatchEvent(event); </code></pre> <p>I've heard such (with no clear definition of 'such') events are 'untrusted', which might explain these errors.</p> <p>The second approach causes whatever value I put as event.which to be passed as event.which, but to no effect (even if I use 98 instead of 9, no 'b' is typed in the box.) If I try setting event.data in the object I'm passing, it ends up undefined when the event is triggered. What follows is the code I'm using to view that:</p> <pre><code>$('#hi').keypress(function(e) { console.log(e); }); </code></pre> <p>Any other ideas?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload