views:

34

answers:

3

I'm trying to change the focus whenever a user presses tab on the last field. I want to set the focus on to another input field.

I have the following javascript code:

$("#input2").keydown(
  function() 
  {
    if(event.which == 9)
    {
      $("#input1").focus();
    }
  }
);

And this is my trial html code:

<div id="inputArea1">
  <input id="input1" />
  <input id="input2" />
</div>

It seems to work with keyup (the changing the focus part) but then again I don't get what I want with keyup..

What am I missing?

+1  A: 

You probably need to cancel the default handling of the event by the browser by returning false from your keydown handler, like this (live example):

$("#input2").keydown(
  function(event) 
  {
    if(event.which == 9)
    {
      $("#input1").focus();
      return false;
    }
  }
);
T.J. Crowder
jQuery passes in the event in the anonymous function, but you're using a global `event` variable
Harmen
@Harmen: Good catch, I actually just copied the OP's handler and added the `return false;`, didn't see he'd missed that out. Fixed.
T.J. Crowder
@T.J. Crowder, just like me, but I changed it later on :P
Harmen
Btw. I noticed that too and have now also my code, so that the event is passed in as parameter.
Mark
+1  A: 

You need to stop the event, by returning false. If you do not, the basic browser event is fired after you switched to input1, which means the focus is back at input2.

For example:

$("#input2").keydown(function(e){
  if(e.which == 9){
    $("#input1").focus();
    return false;
  }
});
Harmen
Thanks that was it! :-) didn't realise that the browser executes the standard operation but it kinda makes sense..
Mark
+1  A: 

Yes, those guys get to it before me.

Another jQuery way is to use event.preventDefault()

$("#input2").keydown(
  function() 
  {
    if(event.which == 9)
    {
      event.preventDefault();
      $("#input1").focus();   
    }
  }
);

Live example: http://jsfiddle.net/ebGZc/1/

動靜能量
Okay that works too, but what should I prefer and why?
Mark
in the jQuery case, I would prefer `event.preventDefault()`, because it is more apparent, and your teammate won't remove or change it that easily as `return false`... the line `return false` look kind of generic and doesn't show to yourself or other coders for its intention.
動靜能量
Ok I see, so in the end it is a question of which style is preferred.. But thanks for showing me the further option.
Mark
@Mark: It's not purely a matter of style, no. With jQuery, `return false;` is equivalent to `event.preventDefault();` (http://api.jquery.com/event.preventDefault/) **plus** `event.stopPropagation()` (http://api.jquery.com/event.stopPropagation/). The former prevents the browser's default action for the event, the latter prevents the event from bubbling further up the DOM tree. Sometimes you want to do one and not the other, which is why they're separate, but if you want to do both (which is common), `return false;` does that. (cont'd)
T.J. Crowder
@Mark: (continuing) This is actually jQuery doing its best to make all browsers behave in accordance with the DOM specification for events (http://www.w3.org/TR/2003/NOTE-DOM-Level-3-Events-20031107/events.html#Events-Event).
T.J. Crowder
@T.J. Crowder: Thx, for taking the time explaining me this. I'm still quite new to JS i.e. JQuery so I'm always happy when someone can give me a deeper insight on what actually really happens when some code is executed.
Mark