Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery Set Focus After Tab Key Press

Tags:

jquery

I'm trying to set the focus to a particular text box after pressing the TAB key. There is only one text box on the page. It seems pretty simple.

$("#status").keydown(function (e) {    
    if (e.which == 9) {
        $("#statuses").html(this.value);        
        this.value = "";        
        $("#status").focus();
    }
});

Here is my jsFiddle example:

http://jsfiddle.net/7Rfqa/

If I code it for the ENTER button it works fine, but for TAB it just goes to the URL bar.

like image 814
Papa Burgundy Avatar asked Jan 08 '14 23:01

Papa Burgundy


Video Answer


1 Answers

You need to stop the default behavior, try this

$("#status").keydown(function (e) {    
  if (e.which == 9) {
    $("#statuses").html(this.value);        
    this.value = "";        
    $("#status").focus();
    e.preventDefault();
  }
});

Fiddle: http://jsfiddle.net/7Rfqa/1/

like image 140
Hattan Shobokshi Avatar answered Sep 20 '22 04:09

Hattan Shobokshi