Forum:WikiaSearch Autofocus

I'm trying to make the search bar automatically focus when something is typed. So I wrote this:

$('*').keypress(function {	$('.WikiaSearch input[type="text"]').focus; } );

But it makes message walls, comments, and any other  or   element pretty much impossible. So I wrote this:

$('input, textarea').unbind;

Only that didn't work. So I tried replacing  with. And that didn't work either, which is why I'm here. Is this solvable, or is it impossible? Thanks in advance. - Sherm / Talk (posted on  12, 2012 at 16:11)


 * Don't use * by itself in jQuery event selectors, ever. The memory usage that causes is insane, you've created something in the order of 10000 copies of that event binding. What you want is to use event bubbling, try this:


 * Most (but not all) events bubble. When you press a key on an element, the event is sent to that element, then it's parent, then that's parent and so on until it reaches the body, document and window. It can be received anywhere along the parent chain unless propagation is stopped. Lunarity 01:43, November 13, 2012 (UTC)


 * Can I have a tl;dr version? :P That is a bit confusing for my simple mind, but I think I understand the gist of how that works. The one thing I don't understand in the code is ; where did that come from!? On the other hand, I completely understand what you meant about , I didn't think about that before. On the practical side of the code, the one unintended difference that I noticed was that the first character that was typed is always missed in the search bar, oddly. Thanks for the help thus far, though, I appreciate it. - Sherm / Talk  (posted on  13, 2012 at 02:39 UTC)


 * I don't really see how I can shorten 2 sentences, it'd be easier to explain graphically. Try googling for "javascript event bubbling tutorial". ".WikiaSearchHack" is an event namespace, it's an arbitrary string I made up to identify the event, you can use it to do  which will remove all events that were created using 'something.WikiaSearchHack' on that element. Doing   would remove everyone's event handlers, not just yours.


 * For me, neither version types into the box. If it does for you then your browser may have a bug. Updated code:


 * This code isn't very reliable though, that exclusion selector isn't specific enough. This will work better:


 * Although, I'm not prepared to explain how or why this last version works as it covers an extremely large area across the DOM, jQuery, jQuery UI and MediaWiki. I could probably teach a week long course on it. Lunarity 03:13, November 13, 2012 (UTC)


 * Actually, that should exclude command keys and tab. Lunarity 03:55, November 13, 2012 (UTC)