Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to avoid to modify the event object in this situation
    primarykey
    data
    text
    <p>This question is related to this one <a href="https://stackoverflow.com/questions/12039219/autocomplete-first-item-focused-only-when-the-user-type-on-tab-key">Autocomplete: first item focused only when the user type on tab key</a>.</p> <p>In order to make the first item focused only when the user type on tab key by using jqueryUi is possible to make something like this <a href="http://jsfiddle.net/uymYJ/8/" rel="nofollow noreferrer">http://jsfiddle.net/uymYJ/8/</a> (1). </p> <p>The bad thing about this implementation is the modification of the event object <code>event.keyCode = $.ui.keyCode.DOWN;</code>.<br> In fact by doing this way, it will impact all other listeners on this event: all listener on the keydown event running later (which includes all delegated listeners) will see the event.keycode as being $.ui.keyCode.ENTER. </p> <p>My questions are:<br> 1) Are my concerns about <code>the modification of the event object</code> justified? If not why?<br> 2) Can you suggest another way to achieve the same result?<br> 3) One opition, as suggested by @AaronDigulla, could be to use <code>document.createEvent()</code>. What is the proper way to use this method in this context? I did try the following code (3) but it does not work.</p> <p>P.S.:<br> Here is the code about autocomplete (2).</p> <hr> <p>(1)</p> <pre><code>$("#box").keydown(function(e){ if( e.keyCode != $.ui.keyCode.TAB) return; e.keyCode = $.ui.keyCode.DOWN; $(this).trigger(e); e.keyCode = $.ui.keyCode.ENTER; $(this).trigger(e); $(this).siblings("input").select(); }); </code></pre> <hr> <p>(2)</p> <pre><code>function (e) { var f = typeof e == "string", g = Array.prototype.slice.call(arguments, 1), h = this; return e = !f &amp;&amp; g.length ? a.extend.apply(null, [!0, e].concat(g)) : e, f &amp;&amp; e.charAt(0) === "_" ? h : (f ? this.each(function () { var d = a.data(this, c), f = d &amp;&amp; a.isFunction(d[e]) ? d[e].apply(d, g) : d; if (f !== d &amp;&amp; f !== b) return h = f, !1 }) : this.each(function () { var b = a.data(this, c); b ? b.option(e || {})._init() : a.data(this, c, new d(e, this)) }), h) } </code></pre> <hr> <p>(3)</p> <pre><code>$("#box").keydown(function(){ e = document.createEvent('KeyboardEvent'); e.initEvent("keydown", true, true); $(this).dispatchEvent(e); if( e.keyCode != $.ui.keyCode.TAB) return; e.keyCode = $.ui.keyCode.DOWN; $(this).trigger(e); e.keyCode = $.ui.keyCode.ENTER; $(this).trigger(e); $(this).siblings("input").select(); }); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

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