views:

41

answers:

2

I have a simple display project and want to cut out the mouse from the equation, the page only has one field (a search field) and I want the browser to be locked into to the field remove the need for a mouse while keeping it user friend for those who don't know how to maneuver without a mouse.

Can it be done with jQuery/Javascript? I don't really know what im search for besides it may have something to do with tab indexing.

Thanks,

Arthur

+1  A: 

if you mean that you want the field item to be focused (so that the user can start typing in it right away) then

$(document).ready(function(){
   $(':text:eq(0)').focus();
});

this will put the focus inside the first input box it will find in the page..

(the :eq(0) can be omitted if you only have one input box ..)

Gaby
I think he also wants to prevent tabbing (or clicking) out of the text box; in other words, he wants to prevent its loss of focus. I'm not sure if the "focus" event bubbles, but if it does then the easiest thing to do is bind the body's focus event to a function that sets focus on the text box.
Pointy
easier would be to use the onblur event for the text box to refocus
Jonathan Fingland
focus doesn't bubble. Easiest method is to do what Jonathan said and use the onblur event to refocus. In IE (maybe others too) you will need to refocus in a timeout with a length of 0.
Andy E
+2  A: 

Since Gaby hasn't edited, I'll go ahead and write up the solution. Some of the credit for the code goes to Gaby and Andy E.

$(document).ready(function(){
   var el = $(':text:eq(0)');
   el.focus();
   el.blur(function(){ 
             setTimeout(function() { el.focus(); },0);
           });
});
Jonathan Fingland
Perfect, simple and effective solution. Thank you.
askon
@jonathan, great ! i missed the locking part. +1
Gaby