admin管理员组

文章数量:1331849

I have a autoplete textbox in a form and I want to detect whether user has focussed on the textbox from navigating through tab key press.I mean tabindex has been set up on different form fields and user can navigate fields by pressing tabs.Now I want to perform some action when user directly mouse click/foxus on the textbox and some other action when user has focussed on the textbox through tab.

Below is the code I was trying.But no matter everytime code is 0.

$('#tbprofession').on('focus', function (e) {
        var code = (e.keyCode ? e.keyCode : e.which);
        if (code == 9) {
            alert('Tabbed');
        }
        else 
        {
            alert('Not tabbed');
        }
});

This code does not work.

Note:Before marking duplicate it will be good if you understand the question correctly.Else I can make it more clear with more elaborated description.

Anyone can show me some light?

I have a autoplete textbox in a form and I want to detect whether user has focussed on the textbox from navigating through tab key press.I mean tabindex has been set up on different form fields and user can navigate fields by pressing tabs.Now I want to perform some action when user directly mouse click/foxus on the textbox and some other action when user has focussed on the textbox through tab.

Below is the code I was trying.But no matter everytime code is 0.

$('#tbprofession').on('focus', function (e) {
        var code = (e.keyCode ? e.keyCode : e.which);
        if (code == 9) {
            alert('Tabbed');
        }
        else 
        {
            alert('Not tabbed');
        }
});

This code does not work.

Note:Before marking duplicate it will be good if you understand the question correctly.Else I can make it more clear with more elaborated description.

Anyone can show me some light?

Share Improve this question edited May 24, 2016 at 17:05 Navoneel Talukdar asked May 24, 2016 at 11:47 Navoneel TalukdarNavoneel Talukdar 4,6085 gold badges23 silver badges42 bronze badges 3
  • Um... why do you think that focus event have keyCode property...? – Michał Perłakowski Commented May 24, 2016 at 11:59
  • Possible duplicate of Detect focus initiated by tab key? – M_T Commented May 24, 2016 at 12:01
  • I know that's why if you notice there is an OR condition.Also I will need to determine it on focus – Navoneel Talukdar Commented May 24, 2016 at 12:01
Add a ment  | 

3 Answers 3

Reset to default 4

You can try something like that :

$(document).on("keyup", function(e) {
  if ($('#tbprofession').is(":focus")) {
    var code = (e.keyCode ? e.keyCode : e.which);
    if (code == 9) {
      alert('I was tabbed!');
    } else {
      alert('not tabbed');
    }
  }
});

fiddle : https://jsfiddle/xc847mrp/

You can use keyup event instead:

$('#tbprofession').on('keyup', function(e) {
  var code = (e.keyCode ? e.keyCode : e.which);
  if (code == 9) {
    console.log('I was tabbed!', code);
  }
});
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input autofocus>
<input id='tbprofession'>

You could have an array of key events triggered anytime a user presses a key while on your page. Although this makes you think of a keylogger. Or just keep the last key. Or a boolean saying if the last key pressed was a TAB or not.

And on focus you can look at that variable.

本文标签: javascriptDetect Tab Key event on textbox on focusStack Overflow