jQuery: How to capture the TAB keypress within a Textbox

JavascriptJquery

Javascript Problem Overview


I want to capture the TAB keypress, cancel the default action and call my own javascript function.

Javascript Solutions


Solution 1 - Javascript

Edit: Since your element is dynamically inserted, you have to use delegated on() as in your example, but you should bind it to the keydown event, because as @Marc comments, in IE the keypress event doesn't capture non-character keys:

$("#parentOfTextbox").on('keydown', '#textbox', function(e) { 
  var keyCode = e.keyCode || e.which; 
 
  if (keyCode == 9) { 
    e.preventDefault(); 
    // call custom function here
  } 
});

Check an example here.

Solution 2 - Javascript

Working example in jQuery 1.9:

$('body').on('keydown', '#textbox', function(e) {
	if (e.which == 9) {
		e.preventDefault();
		// do your code
	}
});

Solution 3 - Javascript

$('#textbox').live('keypress', function(e) {
    if (e.keyCode === 9) {
        e.preventDefault();
        // do work
    }
});

Solution 4 - Javascript

Above shown methods did not work for me, may be i am using bit old jquery, then finally the below shown code snippet works for - posting just in case somebody in my same position

$('#textBox').live('keydown', function(e) {
    if (e.keyCode == 9) {
        e.preventDefault();
        alert('tab');
    }
});

Solution 5 - Javascript

An important part of using a key down on tab is knowing that tab will always try to do something already, don't forget to "return false" at the end.

Here is what I did. I have a function that runs on .blur and a function that swaps where my form focus is. Basically it adds an input to the end of the form and goes there while running calculations on blur.

$(this).children('input[type=text]').blur(timeEntered).keydown(function (e) {
        var code = e.keyCode || e.which;
        if (code == "9") {
            window.tabPressed = true;
            // Here is the external function you want to call, let your external
            // function handle all your custom code, then return false to
            // prevent the tab button from doing whatever it would naturally do.
            focusShift($(this));
            return false;
        } else {
            window.tabPressed = false;
        }
        // This is the code i want to execute, it might be different than yours
        function focusShift(trigger) {
            var focalPoint = false;
            if (tabPressed == true) {
                console.log($(trigger).parents("td").next("td"));
                focalPoint = $(trigger).parents("td").next("td");

            }
            if (focalPoint) {
                $(focalPoint).trigger("click");
            }
        }
    });

Solution 6 - Javascript

Try this:

$('#contra').focusout(function (){
    $('#btnPassword').focus();
});

Solution 7 - Javascript

Suppose you have TextBox with Id txtName

$("[id*=txtName]").on('keydown', function(e) { 
  var keyCode = e.keyCode || e.which; 
  if (keyCode == 9) {
     e.preventDefault();
     alert('Tab Pressed');
 }
}); 

Solution 8 - Javascript

You can capture an event tab using this JQuery API.

$( "#yourInputTextId" ).keydown(function(evt) {
   if(evt.key === "Tab")
      //call your function
});

Solution 9 - Javascript

This worked for me:

$("[id*=txtName]").on('keydown', function(e) { var keyCode = e.keyCode || e.which; if (keyCode == 9) { e.preventDefault(); alert('Tab Pressed'); } });

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJon EricksonView Question on Stackoverflow
Solution 1 - JavascriptChristian C. SalvadóView Answer on Stackoverflow
Solution 2 - JavascriptBuzogany LaszloView Answer on Stackoverflow
Solution 3 - JavascriptJon EricksonView Answer on Stackoverflow
Solution 4 - JavascriptOxiView Answer on Stackoverflow
Solution 5 - JavascriptJordan From Freer ToolView Answer on Stackoverflow
Solution 6 - JavascriptCarlosView Answer on Stackoverflow
Solution 7 - JavascriptUJSView Answer on Stackoverflow
Solution 8 - JavascriptheronsanchesView Answer on Stackoverflow
Solution 9 - JavascriptVentas UruguayView Answer on Stackoverflow